-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
72 lines (59 loc) · 2.06 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
const express = require('express');
const next = require('next');
const path = require('path');
const url = require('url');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const cors = require('cors');
const mongoose = require('mongoose');
const dev = process.env.NODE_ENV !== 'production';
const server = express();
const app = next({ dev });
const handle = app.getRequestHandler();
app
.prepare()
.then(() => {
// api route files
const indexRouter = require('./server/routes/index');
const usersRouter = require('./server/routes/users');
// database connection
mongoose.connect(
'mongodb+srv://rinXer7:[email protected]/test?retryWrites=true&w=majority',
{useNewUrlParser: true, useCreateIndex: true}
)
.then(() => {
console.log('We\'re connected to database! :)');
})
.catch(err => {
console.log('Unable to connect with the database', err);
});
// api routes
server.use('/api', indexRouter);
server.use('/api/users', usersRouter);
// custom routes
server.get('/user/:username', (req, res) => {
const mergedQuery = Object.assign({}, req.query, req.params);
return app.render(req, res, '/user', mergedQuery);
});
server.get('*', (req, res) => {
const parsedUrl = url.parse(req.url, true);
const { pathname } = parsedUrl;
if (pathname === '/service-worker.js') {
const filePath = join(__dirname, '.next', pathname);
app.serveStatic(req, res, filePath);
} else {
handle(req, res, parsedUrl);
}
});
server.use(cors());
server.use(logger('dev'));
server.use(express.json());
server.use(express.urlencoded({ extended: false }));
server.use(cookieParser());
const port = process.env.PORT || 3000;
server.listen(port, err => {
if (err) throw err;
console.log(`> Ready on port ${port}...`);
});
});
//module.exports = server;