Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
69 lines (62 loc) · 1.62 KB
/
Copy pathapp.js
File metadata and controls
69 lines (62 loc) · 1.62 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* Xinkao Temperature Daily Upload
* Single User Function v1.0
* Copyright (c) HZGeek 2022.
* Author: JupiterJun
*/
// 导入包和部分包的实例化
const lib = require("./lib.js");
const Koa = require("koa");
const bodyParse = require("koa-body");
const Router = require("koa-router");
const koa = new Koa();
const app = new Router();
// koa设置默认配置
koa.use(bodyParse());
koa.use(async (ctx, next) => {
ctx.json = (json) => {
ctx.body = JSON.stringify(json);
ctx.type = "application/json";
};
await next();
});
// router处理根路径请求
app.post("/", async (ctx) => {
let data = ctx.request.body;
if (typeof data === "string") {
try {
data = JSON.parse(ctx.request.body);
} catch {
data = null;
}
}
if (!data) return ctx.json({ code: -114514, msg: "请求体JSON解析失败" });
const scf = {
reqId: ctx.request.header["x-scf-request-id"],
region: ctx.request.header["x-scf-region"],
funcName: ctx.request.header["x-scf-name"],
};
console.log(JSON.stringify(data)); // 日志
// 验证请求基本格式
if (
!data.mobile ||
!data.password ||
!data.dorm ||
!data.address ||
!data.headteacher ||
data.childnum === undefined ||
data.childnum === null
) {
return ctx.json({ code: -1, msg: "有项目未填" });
}
if (!/^1\d{10}$/.test(data.mobile)) {
return ctx.json({ code: -2, msg: "手机号格式不对" });
}
let result = await lib(data);
result.scf = scf;
console.log(result);
return ctx.json(result);
});
// koa监听
koa.use(app.routes());
koa.listen(9000, async () => console.log("Server Started."));