Express Get
var express = require('express');var app = express();// respond with "hello world" when a GET request is made to the homepageapp.get('/', function(req, res) { res.send('hello world');});
Express Post
// GET method routeapp.get('/', function (req, res) { res.send('GET request to the homepage');});// POST method routeapp.post('/', function (req, res) { res.send('POST request to the homepage');});
- 有一种特殊路由方法:
app.all(),它并非派生自 HTTP 方法。该方法用于在所有请求方法的路径中装入中间件函数。 - 在以下示例中,无论您使用 GET、POST、PUT、DELETE 还是在 http 模块中支持的其他任何 HTTP 请求方法,都将为针对“/secret”的请求执行处理程序。
app.all('/secret', function (req, res, next) {console.log('Accessing the secret section ...');next(); // pass control to the next handler});
路由路径
- app.route()
- express.router()
参考资料