express 学习笔记(一)路由

先导入express:javascript

var express = require('express'); var app = express();

1.路由方法:html

    getpostputheaddeleteoptionstracecopylockmkcolmovepurgepropfindproppatchunlockreportmkactivitycheckoutmergem-searchnotifysubscribeunsubscribepatchsearch, 和 connectjava

  经常使用的get,post正则表达式

app.get('/', function (req, res) { res.send('GET request to the homepage'); });
特殊的all方法

app.all() 是一个特殊的路由方法,没有任何 HTTP 方法与其对应,它的做用是对于一个路径上的全部请求加载中间件app.all()
app.all('/secret', function (req, res, next) { console.log('Accessing the secret section ...'); next(); // pass control to the next handler });
来自 “/secret” 的请求,无论使用 GET、POST、PUT、DELETE 或其余任何http模块支持的 HTTP 请求,句柄都会获得执行。

2.路由路径
路由路径能够是普通字符串,也能够是正则表达式
普通路径:
// 匹配根路径的请求 app.get('/', function (req, res) { res.send('root'); }); // 匹配 /about 路径的请求 app.get('/about', function (req, res) { res.send('about'); }); // 匹配 /random.text 路径的请求 app.get('/random.text', function (req, res) { res.send('random.text'); });
// 匹配 acd 和 abcd app.get('/ab?cd', function(req, res) { res.send('ab?cd'); }); // 匹配 abcd、abbcd、abbbcd等 app.get('/ab+cd', function(req, res) { res.send('ab+cd'); }); // 匹配 abcd、abxcd、abRABDOMcd、ab123cd等 app.get('/ab*cd', function(req, res) { res.send('ab*cd'); }); // 匹配 /abe 和 /abcde app.get('/ab(cd)?e', function(req, res) { res.send('ab(cd)?e'); });

使用正则表达式的路由路径示例:express


路由句柄
能够为请求处理提供多个回调函数,其行为相似中间件。惟一的区别是这些回调函数有可能调用  方法而略过其余路由回调函数。能够利用该机制为路由定义前提条件
// 匹配任何路径中含有 a 的路径: app.get(/a/, function(req, res) { res.send('/a/'); }); // 匹配 butterfly、dragonfly,不匹配 butterflyman、dragonfly man等 app.get(/.*fly$/, function(req, res) { res.send('/.*fly$/'); });next('route')

使用一个回调函数处理路由:json

app.get('/example/a', function (req, res) { res.send('Hello from A!'); }); 

使用多个回调函数处理路由(记得指定 next 对象):api

app.get('/example/b', function (req, res, next) { console.log('response will be sent by the next function ...'); next(); }, function (req, res) { res.send('Hello from B!'); }); 

使用回调函数数组处理路由:数组

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!'); } app.get('/example/c', [cb0, cb1, cb2]); 

混合使用函数和函数数组处理路由:app



响应方法:
res.download() 提示下载文件。
res.end() 终结响应处理流程。
res.json()发送一个 JSON 格式的响应.
res.jsonp()发送一个支持 JSONP 的 JSON 格式的响应.
res.redirect()重定向请求。
res.render() 渲染视图模板。
res.send()发送各类类型的响应。
res.sendFile()以八位字节流的形式发送文件。
res.sendStatus()设置响应状态代码,并将其以字符串形式做为响应体的一部分发送。

express.Router
可以使用  类建立模块化、可挂载的路由句柄。 实例是一个完整的中间件和路由系统,所以常称其为一个 “mini-app”。
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('response will be sent by the next function ...'); next(); }, function (req, res) { res.send('Hello from D!'); });express.RouterRouter

在 app 目录下建立名为 birds.js 的文件,内容以下:dom

 
var express = require('express'); var router = express.Router(); // 该路由使用的中间件 router.use(function timeLog(req, res, next) { console.log('Time: ', Date.now()); next(); }); // 定义网站主页的路由 router.get('/', function(req, res) { res.send('Birds home page'); }); // 定义 about 页面的路由 router.get('/about', function(req, res) { res.send('About birds'); }); module.exports = router; 
 

而后在应用中加载路由模块:

 
var birds = require('./birds'); ... app.use('/birds', birds);
相关文章
相关标签/搜索