-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
81 lines (69 loc) · 2.05 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Webpack
// var webpack = require('webpack');
// var WebpackDevServer = require('webpack-dev-server');
// var webpackConfig = require('../../webpack.config');
// Express
var express = require("express");
var history = require("connect-history-api-fallback");
var morgan = require("morgan");
var bodyParser = require("body-parser");
var cors = require("cors");
// Express App
var app = express();
// Env
var PORT = process.env.PORT || 3001;
var NODE_ENV = process.env.NODE_ENV || "development";
// Logging
app.use(morgan("dev"));
// Accept Content-Type
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// CORs
var whitelist = ["http://localhost:8080", "http://localhost:3001"];
var corsOptions = {
origin: true,
credentials: true
};
app.use(cors(corsOptionsDelegate));
// Your middleware
app.use(history());
// your api middleware
// var api = require('./todo_api');
// app.use('/api', cors(), api());
//Static files
app.use(express.static("dist"));
/*
// README: Uncomment only if you're not using `npm run server`
// Only use in development
if (NODE_ENV === 'development') {
var server = new WebpackDevServer(webpack(webpackConfig), {
publicPath: '/__build__',
historyApiFallback: false, // won't work due to order
inline: true,
quiet: false,
noInfo: false,
stats: { colors: true }
});
// Webpack express app that uses socket.io
app.use(server.app);
} else {
app.use('/__build__', express.static('__build__'));
}
*/
// Start the server by listening on a port
app.listen(PORT, function() {
console.log("Listen on http://localhost:" + PORT + " in " + NODE_ENV);
});
// helper function to whitelist domains for cors
function corsOptionsDelegate(req, callback) {
var corsOpts;
if (whitelist.indexOf(req.header("Origin")) !== -1) {
// reflect (enable) the requested origin in the CORS response
corsOpts = corsOptions;
} else {
// disable CORS for this request
corsOpts = { origin: false };
}
// callback expects two parameters: error and options
callback(null, corsOpts);
}