-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver-api.js
61 lines (49 loc) · 1.52 KB
/
server-api.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
require('dotenv').config();
const express = require('express');
const app = express();
const morgan = require('morgan');
const bodyParser = require('body-parser');
const socketio = require('socket.io');
const path = require('path');
const cors = require('cors');
const sqlDb = require('./data/sqlDb');
const seedGraph = require('./data/graphSeed.js');
const createApp = () => {
let originsWhiteList = [
'http://localhost:3000',
'https://dev-dayvulge-react.herokuapp.com'
//'https://dev.dayvulge.com'
];
let corsOptions = {
origin: (origin, callback) => {
callback(null, originsWhiteList.indexOf(origin) !== -1)
},
credentials: true
};
app.use(cors(corsOptions));
//app.options('*', cors())
// for server logs to help debugging
app.use(morgan('dev'));
// requests contain a body. If you want to use it in req.body, you will need some body parsing middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}))
// establish routes
app.use('/', require('./api'))
//error handling route
app.use((err, req, res, next) => {
res.status(500).json({error: err});
});
}
// function wrapper around server listen:
const startListening = () => {
const PORT = process.env.PORT || 3001;
const server = app.listen(PORT, function(){
console.log(`Listening on port ${PORT}`)
})
// set up our socket control center
const io = socketio(server)
require('./socket')(io)
}
sqlDb.sync({force: true})
.then(createApp);
seedGraph().then(startListening);