-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
50 lines (38 loc) · 1.15 KB
/
app.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
const Koa = require("koa");
const app = new Koa();
const koaStatic = require("koa-static");
const fs = require("fs");
const exec = require("child_process").exec;
const http = require("http");
const server = http.Server(app.callback());
const socketServer = require("./Server/socketServer.js");
const menu = require("./Server/menuMD.js");
// webSocket
socketServer(server);
// 第一种方式:使用koa-static中间件
app.use(koaStatic(__dirname, { defer: true }));
app.use(async (ctx, next) => {
// 设置 service worker scope 范围
ctx.append("Service-Worker-Allowed", ["/"]);
await next();
});
app.use(menu);
app.use(async (ctx, next) => {
console.log(ctx.url);
await next();
});
// 第二种方式:设置响应
// app.use((ctx) => {
// console.log(ctx.url);
// const typeMap = {
// js: "text/javascript",
// };
// ctx.type = typeMap[ctx.url.split(".").pop()] || "text/html";
// if (ctx.url === "/") ctx.url = "/index.html";
// ctx.body = fs.readFileSync(__dirname + ctx.url, "utf8");
// });
server.listen(4000, () => {
console.log("listening port 4000");
// 打开默认浏览器
// exec(" open http://localhost:4000")
});