-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
46 lines (38 loc) · 1.13 KB
/
app.js
File metadata and controls
46 lines (38 loc) · 1.13 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
/**
* Module dependencies and constant configs
*/
const path = require('path');
const express = require('express');
const ROOT_DIR = path.resolve(__dirname, 'public');
const PORT = process.env.PORT || 4200;
/**
* Initiate an express app
*/
const app = express();
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'Get, PUT, POST, DELETE, PATCH, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept, Observe, Authorization');
res.setHeader('Access-Control-Allow-Credentials', true);
if (req.method === 'OPTIONS') {
res.status(200);
res.end();
} else {
next();
}
});
app.use(express.static(ROOT_DIR));
/**
* Configure routes
*/
app.get('/', (req, res) => res.sendFile('index.html', { root: ROOT_DIR }));
/**
* Handle errors by adding it as a middleware
*/
app.use((err, req, res, next) => {
res.status(err.status || 500).send('Some error has occured.');
});
/**
* Start the app by listening to a specific port
*/
app.listen(PORT, () => console.log('Server running at port: ' + PORT));