Express4中文文档
路由方法派生自 HTTP 方法之一,并附加到 express
类的实例。
以下代码是为应用程序根目录的 GET 和 POST 方法定义的路由示例。
// GET method route
app.get('/', (req, res) => {
res.send('GET request to the homepage')
})
// POST method route
app.post('/', (req, res) => {
res.send('POST request to the homepage')
})
Express 支持所有 HTTP 请求方法对应的方法:get
、post
等。如需完整列表,请参阅 app.METHOD。
有一种特殊的路由方法,app.all()
,用于在所有 HTTP 请求方法的路径上加载中间件功能。例如,无论使用 GET、POST、PUT、DELETE 还是 http 模块 中支持的任何其他 HTTP 请求方法,都会对路由 "/secret" 的请求执行以下处理程序。
app.all('/secret', (req, res, next) => {
console.log('Accessing the secret section ...')
next() // pass control to the next handler
})