-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
119 lines (96 loc) · 2.75 KB
/
app.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
var express = require("express");
const res = require("express/lib/response");
const req = require("express/lib/request");
var birds = require('./birds')
var app = express();
app.get("/", function (req, res) {
console.log(req, res);
res.send("Hello World!");
});
app.get("/user", function (req, res) {
res.send("Got a Get request");
});
// 网站首页接受 POST 请求
app.post("/user", function (req, res) {
res.send("Got a POST request");
});
// /user 节点接受 PUT 请求
app.put("/user", function (req, res) {
res.send("Got a PUT request at /user");
});
// /user 节点接受 DELETE 请求
app.delete("/user", function (req, res) {
res.send("Got a DELETE request at /user");
});
/**
* app.all() 是一个特殊的路由方法,没有任何 HTTP 方法与其对应,它的作用是对于一个路径上的所有请求加载中间件。
* 在下面的例子中,来自 “/secret” 的请求,不管使用 GET、POST、PUT、DELETE 或其他任何 http 模块支持的 HTTP 请求,句柄都会得到执行。
*/
app.all("/secret", function (req, res, next) {
console.log("Accessing the secret section ...");
next(); // pass control to the next handler
});
// 匹配 abcd、abbcd、abbbcd等
app.get("/ab+cd", function (req, res) {
res.send("ab+cd");
});
// 匹配 abcd、abxcd、abRABDOMcd、ab123cd等
app.get("/ab*cd", function (req, res) {
res.send("ab*cd");
});
// 匹配 /abe 和 /abcde
app.get("/ab(cd)?e", function (req, res) {
res.send("ab(cd)?e");
});
// 匹配任何路径中含有 a 的路径:
app.get(/match/, function (req, res) {
res.send("/a/");
});
// 匹配 butterfly、dragonfly,不匹配 butterflyman、dragonfly man等
app.get(/.*fly$/, function (req, res) {
res.send("/.*fly$/");
});
app.get("/example/a", function (req, res) {
res.send("Hello from A!");
});
app.get(
"/example/b",
function (req, res, next) {
console.log("response will be sent by the next function ...");
next();
},
function (req, res) {
res.send("Hello from B!");
}
);
var cb0 = function (req, res, next) {
console.log("CB0");
next();
};
var cb1 = function (req, res, next) {
console.log("CB1");
next();
};
var cb2 = function (req, res) {
res.send("Hello from C!");
};
app.get("/example/c", [cb0, cb1, cb2]);
app.get(
"/example/d",
[cb0, cb1],
function (req, res, next) {
console.log("the response will be sent by the next function ...");
next();
},
function (req, res) {
res.send("Hello from D!");
}
);
app.use('/birds', birds)
// app.use(express.static('public'))
app.use('/static', express.static('public'))
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log("Example app listening at http://%s:%s", host, port);
});