-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
55 lines (39 loc) · 1.25 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
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var path = require('path');
var app = express();
mongoose.set('useFindAndModify', false);
mongoose.connect('mongodb://localhost/movielist', {
useNewUrlParser : true
});
var db = mongoose.connection;
db.on('error', err => console.log(err))
var allowCrossDomain = function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Cache-Control");
// intercept OPTIONS method
if ('OPTIONS' == req.method) {
res.sendStatus(200);
}
else {
next();
}
};
app.use(allowCrossDomain);
// middleware
app.use(bodyParser.json());
var port = 4000;
var movies = require('./routes/index');
app.get('/', function(req, res){
res.send('Welcome to Movielist');
})
app.use('/api', movies)
if (process.env.NODE_ENV === 'production'){
app.set(express.static('client/dist'));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'dist', 'index.html'));
});
}
app.listen(port, () => {`Server starts at port ${port}`});