-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
54 lines (44 loc) · 1.59 KB
/
Copy pathserver.js
File metadata and controls
54 lines (44 loc) · 1.59 KB
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
const Koa = require("koa");
const Router = require("@koa/router");
const https = require("https");
const fs = require("fs");
const next = require("next");
const config = require("./assets/json/config/common.json");
const host = process.env.HOST || "127.0.0.1";
const dev = process.env.NODE_ENV !== "production";
const app = next({dev});
const handle = app.getRequestHandler();
app.prepare().then(() => {
const server = new Koa();
const router = new Router();
// koa router 9.0.0 or more
router.all("(.*)", async (ctx) => {
ctx.respond = false;
await handle(ctx.req, ctx.res);
});
server.use(async (ctx, next) => {
ctx.res.statusCode = 200;
await next();
});
server.use(router.routes());
try{
const options = {
key: fs.readFileSync("./private.key"),
cert: fs.readFileSync("./certificate.crt"),
ca: fs.readFileSync("./ca_bundle.crt")
};
server.listen(3000, host);
https.createServer(options, server.callback()).listen(8888, host, () => {
console.log("> Ready on http://localhost:3000");
console.log("> Ready on https://localhost:8888");
console.log(`> NEXT_PUBLIC_ENV: ${process.env.NEXT_PUBLIC_ENV}`);
console.log(`> Version: ${config.version}`);
});
}
catch(ex){
server.listen(3000, host);
console.log("> Ready on http://localhost:3000");
console.log(`> NEXT_PUBLIC_ENV: ${process.env.NEXT_PUBLIC_ENV}`);
console.log(`> Version: ${config.version}`);
}
});