-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
84 lines (61 loc) · 2.08 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//~ Import Dotenv
import 'dotenv/config';
//~ Import Express
import express from 'express';
const app = express();
//~ Import modules
import { router } from './app/routes/index.js';
import { ErrorApi } from './app/services/errorHandler.js';
//~ Protect API
import helmet from 'helmet';
app.use(helmet());
//~ IMPORTATION SWAGGER DOCS
import { specs, serve, setup, cssOptions} from './app/swaggerDocs/swaggerDocs.js';
app.use('/api-docs', serve, setup(specs, cssOptions));
//~ Import Debug
import debug from 'debug';
const logger = debug('EntryPoint');
//~ Encoding
//accept Content-type: application/json
app.use(express.json());
// accept Content-type: application/x-www-form-urlencoded
app.use(express.urlencoded({
extended: false
}));
//~ Cors
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
res.setHeader('Access-Control-Allow-Methods', 'GET,HEAD,OPTIONS,POST,PUT,PATCH,DELETE');
// res.setHeader('Access-Control-Allow-Headers', 'Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers');
next();
});
//If you have your node.js behind a proxy and are using secure: true, you need to set 'trust proxy' in express
app.set('trust proxy', 1)
// trust first proxy if deploy Heroku
//~ Session
import session from 'express-session';
app.use(session({
saveUninitialized: true,
resave: true,
proxy: true,
secret: process.env.SESSION_SECRET,
cookie: {
httpOnly:true,
secure : true,
sameSite: 'lax', // or 'strict'
maxAge: 24 * 60 * 60 * 1000 //24 hours
//expires : new Date(Date.now() + 60 * 60 * 1000) //1 hour
}
}));
//~ Router
app.use(router);
//~ Error 404 NOT Found
app.use((req, res)=>{
throw new ErrorApi(`Page not found !`, req, res, 404);
});
//~ Launch Server
const PORT = process.env.PORT ?? 3000;
app.listen(PORT, () => {
logger(`🚀\x1b[1;35m Launch server on http://localhost:${PORT}\x1b[0m`);
});