-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
35 lines (27 loc) · 929 Bytes
/
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
const next = require('next');
const express = require('express');
const routes = require('./routes');
// const host = process.env.HOST || 'localhost';
const port = parseInt(process.env.PORT, 10) || 3000;
const dev = process.env.NODE_ENV !== 'production';
// console.log(process.env)
const app = next({ dev });
const handler = routes.getRequestHandler(app);
app.prepare().then(() => {
const server = express();
// Resolve trailing slash
server.use((req, res, _next) => {
if (req.path.substr(-1) === '/' && req.path.length > 1) {
const query = req.url.slice(req.path.length);
res.redirect(301, req.path.slice(0, -1) + query);
} else {
_next();
}
});
server.use(handler);
server.listen(port, (error) => {
if (error) throw error;
console.log(`🌎 > Ready on port ${port}`); // eslint-disable-line no-console
});
})
.catch(console.error);