-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
54 lines (42 loc) · 1.42 KB
/
server.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
// server.js
// set up ============================================
const express = require('express');
const bodyParser = require('body-parser');
const enforce = require("express-sslify");
const compression = require('compression');
// Init App
const app = express();
// Compress all responses
app.use(compression());
// redirect http requests to https
if (process.env.NODE_ENV === 'production')
app.use(enforce.HTTPS({ trustProtoHeader: true }));
// Support webpack-dev-server
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "http://localhost:5000");
res.header("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE");
res.header("Access-Control-Allow-Headers", "Content-Type, Authorization");
next();
});
// Body Parser Middleware
// parse application/json
app.use(bodyParser.json());
// parse application/x-www-form-urlencoded
// for easier testing with Postman or plain HTML forms
app.use(bodyParser.urlencoded({
extended:true
}));
// Set Static Folder
app.use(express.static('./*.html'));
app.use(express.static('dist'));
//------------------------------------------------------------------//
// Set Port
app.set('port', process.env.PORT || 3000);
app.get('*', (req, res) => {
res.sendFile(`${__dirname}/dist/index.html`);
});
// Launch
app.listen(app.get('port'), () => {
console.log('Server.js started listening on port localhost:' + app.get('port'));
});
module.exports = app;