-
Notifications
You must be signed in to change notification settings - Fork 165
/
index.js
61 lines (52 loc) · 1.24 KB
/
index.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
51
52
53
54
55
56
57
58
59
60
61
const path = require("path");
const express = require("express");
const cors = require("cors");
const morgan = require("morgan");
const { init: initDB, Counter } = require("./db");
const logger = morgan("tiny");
const app = express();
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(cors());
app.use(logger);
// 首页
app.get("/", async (req, res) => {
res.sendFile(path.join(__dirname, "index.html"));
});
// 更新计数
app.post("/api/count", async (req, res) => {
const { action } = req.body;
if (action === "inc") {
await Counter.create();
} else if (action === "clear") {
await Counter.destroy({
truncate: true,
});
}
res.send({
code: 0,
data: await Counter.count(),
});
});
// 获取计数
app.get("/api/count", async (req, res) => {
const result = await Counter.count();
res.send({
code: 0,
data: result,
});
});
// 小程序调用,获取微信 Open ID
app.get("/api/wx_openid", async (req, res) => {
if (req.headers["x-wx-source"]) {
res.send(req.headers["x-wx-openid"]);
}
});
const port = process.env.PORT || 80;
async function bootstrap() {
await initDB();
app.listen(port, () => {
console.log("启动成功", port);
});
}
bootstrap();