-h, --help output usage information --version output the version number -e, --ejs add ejs engine support --hbs add handlebars engine support --pug add pug engine support -H, --hogan add hogan.js engine support --no-view generate without view engine -v, --view <engine> add view <engine> support (ejs|hbs|hjs|jade|pug|twig|vash) (defaults to jade) -c, --css <engine> add stylesheet <engine> support (less|stylus|compass|sass) (defaults to plain css) --git add .gitignore -f, --force force on non-empty directory
app.get("/example/a", function (req, res) { res.send("Hello from A!"); });
多个回调函数可以处理一个路由(确保您指定 next 对象)。例如:
1 2 3 4 5 6 7 8 9 10
app.get( "/example/b", function (req, res, next) { console.log("the response will be sent by the next function ..."); next(); }, function (req, res) { res.send("Hello from B!"); } );
一组回调函数可以处理一个路由。例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
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!"); };
var cb0 = function (req, res, next) { console.log("CB0"); next(); };
var cb1 = function (req, res, next) { console.log("CB1"); next(); };
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!"); } );
var express = require("express"); var router = express.Router();
// middleware that is specific to this router //use是不管get还是post都可以匹配 router.use(functiontimeLog(req, res, next) { console.log("Time: ", Date.now()); next(); }); // define the home page route router.get("/", function (req, res) { res.send("Birds home page"); }); // define the about route router.get("/about", function (req, res) { res.send("About birds"); });
module.exports = router;
接着,在应用程序中装入路由器模块:
1 2 3
var birds = require('./birds'); ... app.use('/birds', birds);