-
Notifications
You must be signed in to change notification settings - Fork 12
/
server.js
59 lines (48 loc) · 1.63 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
56
57
const express = require('express');
const server = express();
const bodyParser = require('body-parser');
const next = require('next')
const PORT = parseInt(process.env.PORT, 10) || 3000;
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler();
const cors = require('cors');
const mongoose = require('mongoose');
const config = require('./src/api/DB.js');
const userRoutes = require('./src/api/route');
mongoose.Promise = global.Promise;
const connectWithRetry = () => {
mongoose.connect(config.DB, { useNewUrlParser: true }).then(
() => { console.log('Database is connected') },
err => {
console.log('Can not connect to the database' + err)
setTimeout(connectWithRetry, 5000)
}
);
}
connectWithRetry();
app.prepare().then(() => {
server.get('/', (req, res) => {
return app.render(req, res, '/index', req.query)
})
server.get('/create', (req, res) => {
return app.render(req, res, '/create', req.query)
})
server.get('/login', (req, res) => {
return app.render(req, res, '/login', { id: req.params.id })
})
server.get('/export', (req, res) => {
return app.render(req, res, '/export', { id: req.params.id })
})
if (dev) server.use(cors());
server.use(bodyParser.urlencoded({ extended: true }));
server.use(bodyParser.json());
server.use('/users', userRoutes)
server.all('*', (req, res) => {
return handle(req, res)
})
server.listen(PORT, err => {
if (err) throw err
console.log(`> Ready on http://localhost:${PORT}`)
})
})