-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
72 lines (57 loc) · 1.8 KB
/
Copy pathapp.js
File metadata and controls
72 lines (57 loc) · 1.8 KB
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
const express = require('express');
const http = require('http');
const cors = require('cors');
const router = require('./routes/index');
const config = require('./config');
const socketio = require('./sockets/index');
const app = express();
const init = async () => {
if (!global.process.env.PORT) {
return global.console.error('PORT not present in the environment');
}
/*
* Middleware priority
*
* 0. Security (CORS, other headers)
* 1. View Engine
* 2. JSON parser
* 3. URLEncoded parser
* 4. Static generator
* 5. Router
* 6. Socket config
* 7. Server Start
*/
app.set('trust proxy', 1);
let allowlist = [
'https://hooliapi.snu-labyrinth.tech',
'http://localhost:3001',
'http://127.0.0.1:5500',
];
let corsOptionsDelegate = function (req, callback) {
let corsOptions;
if (allowlist.indexOf(req.header('Origin')) !== -1) {
corsOptions = { origin: true }; // reflect (enable) the requested origin in the CORS response
} else {
corsOptions = { origin: false }; // disable CORS for this request
}
callback(null, corsOptions); // callback expects two parameters: error and options
};
app.use(cors());
app.set('view engine', 'ejs');
app.disable('X-Powered-By');
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use('/public', express.static('public'));
app.use('/', router.router);
// Server set
const httpServer = http.createServer(app);
// Sockets
socketio.socketConnection(httpServer);
httpServer.listen(config.port);
httpServer.on('listening', () => {
global.console.log(`Now tuning into port: ${config.port}`);
});
};
module.exports = {
init,
};