-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
57 lines (50 loc) · 1.33 KB
/
app.js
File metadata and controls
57 lines (50 loc) · 1.33 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
// @copyright Hidden Moss Inc.
// @see https://hiddenmoss.com/
// @author Yuancheng Zhang
const express = require("express");
const ctrl = require("./controllers");
const app = express();
app.use(express.json());
app.get(`/`, (req, res) => {
res.redirect("http://hiddenmoss.com");
});
app.post(`/webhook/:from/:to/:bot`, (req, res) => {
const from = req.params.from.toLowerCase();
const to = req.params.to.toLowerCase();
const bot = req.params.bot;
const parser = ctrl.parsers[from];
const template = ctrl.templates[to];
// TODO: need to optimize
if (parser && template) {
try {
const ctx = parser.parse(req);
if (ctx) {
template.send(bot, ctx);
res.status(200).send("OK");
} else {
res.status(400).send("Parse Error");
}
} catch (err) {
console.log(err);
res.status(400).send(err);
} finally {
return;
}
} else {
console.log(req.body);
console.log(parser, template);
res.status(400).send("Link Error");
}
});
app.get("/404", (req, res) => {
res.status(404).send("Not found");
});
// Error handler
app.use(function (err, req, res, next) {
console.error(err);
res.status(400).send("Bad Request");
});
// Web 类型云函数,只能监听 9000 端口
app.listen(9000, () => {
console.log(`Server start on http://localhost:9000`);
});