-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
57 lines (51 loc) · 1.75 KB
/
Copy pathserver.js
File metadata and controls
57 lines (51 loc) · 1.75 KB
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
const express = require("express")
const app = express()
const http = require('http').Server(app)
const initRoutes = require("./routes/web")
const initSockets = require("./routes/socket")
const bodyParser = require('body-parser')
const mongodb = require('mongodb')
const cors = require('cors')
const io = require('socket.io')(http, { origins: '*:*'})
const allowedOrigins = [
'http://localhost:3000',
'http://0.0.0.0:8000',
'http://localhost:8080',
'https://projective.herokuapp.com',
'https://projectiveapi.herokuapp.com'
]
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json({ type: 'application/json' }))
app.use(express.urlencoded({ extended: true }))
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*") // update to match the domain you will make the request from
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
next()
})
app.use(cors({
origin: function(origin, callback){
// allow requests with no origin
// (like mobile apps or curl requests)
if(!origin) {
console.log("not allowed origin to unknown")
return callback(null, true)
}
if(allowedOrigins.includes(origin) === -1){
var msg = 'The CORS policy for this site does not ' +
'allow access from the specified Origin.'
return callback(new Error(msg), false)
}
return callback(null, true)
}
}))
mongodb.MongoClient.connect(process.env.MONGO_URL, { useUnifiedTopology: true, useNewUrlParser: true }, function(err, database) {
if(err) throw err
const db = database.db(process.env.MONGO_URL.split('/').reverse()[0])
const port = process.env.PORT||3000
app.db = db
initRoutes(app)
initSockets(io, db)
http.listen(port, () => {
console.log(`Server running at localhost:${port}`)
})
})