-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
44 lines (33 loc) · 916 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
36
37
38
39
40
41
42
43
44
const express = require("express");
const next = require("next");
const { sequelize, defaultRows } = require("./models");
const dev = process.env.NODE_ENV !== "production";
const port = process.env.PORT || "3000";
const app = next({ dev });
const handle = app.getRequestHandler();
app.prepare().then(async () => {
// Initialize Database
await sequelize.sync();
await defaultRows();
// Initialize Express
const server = express();
// To retrieve HTTP protocol
server.enable("trust proxy");
// Routes
server.all("*", (req, res, next) => {
// Append next
res.next = next;
handle(req, res);
});
// Error Handler
server.use(function (err, req, res, next) {
if (res.headersSent) {
return next(err);
}
res.status(500).json(err);
});
server.listen(port, (err) => {
if (err) throw err;
console.log(`> Ready on http://localhost:${port}`);
});
});