forked from WeixinCloud/wxcloudrun-express
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
60 lines (49 loc) · 1.07 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
const path = require("path");
const express = require("express");
const cors = require("cors");
const morgan = require("morgan");
const logger = morgan("tiny");
const app = express();
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(cors());
app.use(logger);
let count = 0
// 首页
app.get("/", async (req, res) => {
res.sendFile(path.join(__dirname, "index.html"));
});
// 更新计数
app.get("/api/inc", async (req, res) => {
res.send({
code: 0,
data: ++count,
});
});
app.get("/api/clear", async (req, res) => {
count = 0
res.send({
code: 0,
data: count,
});
});
// 获取计数
app.get("/api/count", async (req, res) => {
res.send({
code: 0,
data: count,
});
});
// 小程序调用,获取微信 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.SERVER_PORT || 8080;
async function bootstrap() {
app.listen(port, () => {
console.log("启动成功", port);
});
}
bootstrap();