-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
36 lines (28 loc) · 1.08 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
const path = require('path')
const express = require('express')
const app = express()
const api = require('./server/routes/api')
const mongoose = require('mongoose')
app.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS')
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With')
next()
})
app.use(express.static(path.join(__dirname, 'build')));
app.use('/api/', api)
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
const port = process.env.PORT || 80
const DBname = 'BargigShop'
const MongoDBUri = process.env.MONGODB_URI || `mongodb://localhost/${DBname}`
console.log(port, MongoDBUri, process.env)
mongoose.connect(MongoDBUri, {
useUnifiedTopology: true,
useNewUrlParser: true
})
.catch(e => { console.log(e); process.exit() })
.then(() => {
app.listen(port, () => console.log(`Running server on port ` + port))
})