-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
76 lines (59 loc) · 1.81 KB
/
app.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
require('dotenv').config()
let express = require('express')
let bodyParser = require('body-parser')
let cors = require('cors')
var app = express(),
server = require('http').Server(app),
io = require('socket.io')(server);
var mongoose = require('mongoose')
//app.use(cors())
// Connect to mongoDB.
mongoose.Promise = require('q').Promise
mongoose.connect(process.env.DB_HOST).then((db) => {
// parse requests of content-type - application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }))
// parse requests of content-type - application/json
app.use(bodyParser.json())
// static files permitted
app.use('/static', express.static('static'))
// CORS.
useCors(app)
// Routes for Associates API.
require('./server/routes/image.js')(app) // Order changed for testing without login, using middleware pending
require('./server/routes/user.js')(app)
app.disable('etag')
app.get('*', function (req, res, next) {
res.setHeader('Last-Modified', (new Date()).toUTCString())
next()
})
// Change for integrating socket.io
server.listen(process.env.PORT, function () {
console.log('Server and listening on port ' + process.env.PORT)
})
}).catch(err => { // mongoose connection error will be handled here
console.error('App starting error:', err.stack)
process.exit(1)
})
// Use Cors.
function useCors (app) {
// Cors
var originsWhitelist = [
'http://localhost',
'http://localhost:3000',
'http://localhost:8080'
]
var corsOptions = {
origin: function (origin, callback) {
var isWhitelisted = originsWhitelist.indexOf(origin) !== -1
callback(null, isWhitelisted)
},
credentials: true
}
// Here is the magic
app.use(cors(corsOptions))
}
// For testing purposes
module.exports = {
app,
io
}