-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.ts
59 lines (50 loc) · 1.6 KB
/
server.ts
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
import Express from "express";
import next from "next";
import morgan from "morgan";
import cookieParser from "cookie-parser";
import expressSession from "express-session";
import dotenv from "dotenv";
import chalk from "chalk";
import path from "path";
import { parse } from "url";
dotenv.config();
const __DEV__ = process.env.NODE_ENV === "development";
const app = next({ dev: __DEV__ });
const handle = app.getRequestHandler();
/**
* Connect Express and Next
*/
app.prepare().then(() => {
const server = Express();
server.use("/", Express.static(path.join(__dirname, "public")));
if (__DEV__) {
server.use(morgan("dev"));
}
server.use(Express.json());
server.use(Express.urlencoded({ extended: true }));
server.use(cookieParser(process.env.COOKIE_SECRET));
server.use(
expressSession({
resave: false,
saveUninitialized: false,
secret: process.env.COOKIE_SECRET,
cookie: {
httpOnly: true,
secure: false
}
})
);
server.get("*", (req: Express.Request, res: Express.Response) => {
const parsedURL = parse(req.url, true);
return handle(req, res, parsedURL);
});
server.get("/post/:postId", (req: Express.Request, res: Express.Response) => {
return app.render(req, res, "/post", { postId: req.params.postId });
});
server.listen(3060, () => {
console.log(
chalk.bgBlue.black("START"),
`Next server running on port 3060 on ${__DEV__ ? "development" : "production"} environment`
);
});
});