forked from MrXujiang/h5-Dooring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
50 lines (44 loc) · 1.2 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
const Koa = require("koa");
const { resolve } = require("path");
const staticServer = require("koa-static");
const koaBody = require("koa-body");
const cors = require("koa2-cors");
const logger = require("koa-logger");
const app = new Koa();
app.use(staticServer(resolve(__dirname, "./static")));
app.use(koaBody());
app.use(logger());
// 设置跨域
app.use(
cors({
origin: function(ctx) {
if (ctx.url.indexOf("/dooring") > -1) {
return "*"; // 允许来自所有域名请求
}
return "";
},
exposeHeaders: ["WWW-Authenticate", "Server-Authorization", "x-test-code"],
maxAge: 5, // 该字段可选,用来指定本次预检请求的有效期,单位为秒
credentials: true,
allowMethods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
allowHeaders: [
"Content-Type",
"Authorization",
"Accept",
"x-requested-with",
"Content-Encoding"
]
})
);
let htmlStr = "";
app.use(async (ctx, next) => {
console.log(ctx.url);
if (ctx.url === "/dooring/render") {
htmlStr = ctx.request.body;
ctx.body = "success";
} else if (ctx.url.indexOf("/html") === 0) {
ctx.type = "html";
ctx.body = htmlStr;
}
});
app.listen(3000);