-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
67 lines (54 loc) · 1.54 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
58
59
60
61
62
63
64
65
66
67
require("babel-polyfill");
import * as mobxReact from "mobx-react";
import express from "express";
import next from "next";
const port = parseInt(process.env.PORT, 10) || 3000;
const dev = process.env.NODE_ENV !== "production";
const app = next({ dev });
const handle = app.getRequestHandler();
import cookieSession from "cookie-session";
import bodyParser from "body-parser";
require("dotenv").load();
mobxReact.useStaticRendering(true);
app.prepare().then(() => {
const server = express();
server.use(
cookieSession({
name: "session",
secret: process.env.SECRET,
maxAge: 24 * 60 * 60 * 1000
})
);
server.use(bodyParser.json());
server.use(bodyParser.urlencoded({ extended: true }));
server.get("/", (req, res) => {
return app.render(req, res, "/", req.params);
});
server.post("/api/login", (req, res) => {
const $ = req.body;
if ($.user) {
req.session.user = $.user;
res.send(true);
} else {
res.send(false);
}
});
server.post("/api/logout", (req, res) => {
req.session = null;
res.send(true);
});
server.get("/hdiw", (req, res) => {
return app.render(req, res, "/hdiw", req.params);
});
server.get("/page1", (req, res) => {
return app.render(req, res, "/page1", req.params);
});
server.get("/page2", (req, res) => {
return app.render(req, res, "/page2", req.params);
});
server.get("*", (req, res) => handle(req, res));
server.listen(port, err => {
if (err) throw err;
console.log(`> Ready on http://localhost:${port}`);
});
});