可以参考Express官网关于路由一节:http://expressjs.com/guide/routing.html
1:通过使用GET、POST方式定义主页路由,app.js:
var express = require('express'); var app = express(); app.get('/', function(req,res){ res.send('欢迎来到Express首页'); }); app.post('/', function(req,res){ res.send('Welcome to Express Index.'); });
2:路由路径以及参数定义
var express = require('express'); var app = express(); app.get('/users', function(req,res){ res.send('获取用户列表'); }); app.get('/users/:id', function(req,res){ res.send('user id:'+req.params.id); });
浏览器中输入:http://127.0.0.1:3000/users/,http://127.0.0.1:3000/users/111
3:正则路由,值匹配方式与正则表达式一致,例如:
var express = require('express'); var app = express(); app.get('/ho?l', function(req,res){ res.send('hl,hol'); }); app.get('/ho+l', function(req,res){ res.send('hol,hool,hooool,and so on'); }); app.get('/ho*l', function(req,res){ res.send('hol,holl,hoal,ho1000l,and so on'); }); app.get('/ho(ab)?l', function(req,res){ res.send('hol,habl'); });
4:Route Handler:next()
var express = require('express'); var app = express(); app.get('/a/b', function(req,res,next){ console.log('response will be send by the next function...'); next(); },function(req,res){ res.send('/a/b'); });
注意在next()所在的路由中,不能有输出,要在最后的处理函数中输出,否则后台会输出以下错误内容:
Error: Can't set headers after they are sent. at ServerResponse.OutgoingMessage.setHeader (http.js:689:11) at ServerResponse.header (/home/y/my_note/nodejs/myapp/node_modules/express/lib/response.js:662:10) at ServerResponse.send (/home/y/my_note/nodejs/myapp/node_modules/express/lib/response.js:146:12) at fn (/home/y/my_note/nodejs/myapp/node_modules/express/lib/response.js:896:10) at View.exports.renderFile [as engine] (/home/y/my_note/nodejs/myapp/node_modules/jade/lib/jade.js:325:12) at View.render (/home/y/my_note/nodejs/myapp/node_modules/express/lib/view.js:76:8) at Function.app.render (/home/y/my_note/nodejs/myapp/node_modules/express/lib/application.js:527:10) at ServerResponse.res.render (/home/y/my_note/nodejs/myapp/node_modules/express/lib/response.js:900:7) at module.exports (/home/y/my_note/nodejs/myapp/app.js:90:9) at Layer.handle_error (/home/y/my_note/nodejs/myapp/node_modules/express/lib/router/layer.js:58:5)
5:Response Methods
Method | Description |
---|---|
res.download() | Prompt a file to be downloaded. |
res.end() | End the response process. |
res.json() | Send a JSON response. |
res.jsonp() | Send a JSON response with JSONP support. |
res.redirect() | Redirect a request. |
res.render() | Render a view template. |
res.send() | Send a response of various types. |
res.sendFile | Send a file as an octet stream. |
res.sendStatus() | Set the response status code and send its string representation as the response body. |
6:app.route()
app.route('/book') .get(function(req,res){ res.send('get a book'); }) .post(function(req,res){ res.send('add a book'); }) .put(function(req,res){ res.send('update the book'); });
7:Express.Router
app.js
var routes = require('./routes/index'); var users = require('./routes/users');
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use('/', routes); app.use('/users', users);
index.js
var express = require('express'); var router = express.Router(); /* GET home page. */ router.get('/', function(req, res) { res.render('index', { title: 'Express!' }); }); module.exports = router;