routing:就是响应客户的方法(对应http 的方法)
Basic routing
Routing :一个应用决定如何响应特定终端一个客户请求,特定终端一般是浏览器,输入 URI (or 路径) ,并且对应HTTP 请求方法(GET, POST, and so on).
每一个路由能有一个或更多的处理函数,当路由匹配时执行.
路由定义有如下结构:
app.METHOD(PATH, HANDLER)
Where:
app
是express的一个实例
.METHOD
is an HTTP 请求的方法, 小写.PATH
是浏览器输入的地址.HANDLER
是一个当路由匹配时执行的函数。.
This tutorial assumes that an instance of express
named app
is created and the server is running.
The following examples illustrate defining simple routes.
在主页上,回复一个 Hello World!
app.get('/', function (req, res) {
res.send('Hello World!')
})
在主页上根目录回复一个POST 请求:
app.post('/', function (req, res) {
res.send('Got a POST request')
})
在主页上/user目录回复一个PUT请求:
Respond to a PUT request to the /user
route:
app.put('/user', function (req, res) {
res.send('Got a PUT request at /user')
})
Respond to a DELETE request to the /user
route:
app.delete('/user', function (req, res) {
res.send('Got a DELETE request at /user')
})