个人博客

A web front-end programmer's personal blog.

Express API 测试用例整理(二)

Express API 翻译版本点击这里,本文主要对一些方法提供简单的示例,帮助理解使用。

###Request部分

####req.params

这是一个数组对象,命名过的参数会以键值对的形式存放。 比如你有一个路由/user/:name, “name”属性会存放在req.params.name. 这个对象默认为 {}。当使用正则表达式定义路由的时候,req.params[N]会是这个应用这个正则后的捕获分组, N 是代表的是第N个捕获分组。这个规则同样适用于全匹配的路由,如 /file/*

var express = require('express');
var app = express();
app.get('/user/:name',function(req,res){
	console.log(req.params.name); //blue
	res.json(req.params);  // {"name":"blue"}
});
app.get('/file/*',function(req,res){
	res.json(req.params);  // {"0" : "javascript/jquery.js"}
});
app.listen(3000);
//=> 分别访问 localhost:3000/user/blue , localhost:3000/file/javascript/jquery.js

####req.query

这是一个解析请求参数的对象,默认为{}。如一个类似的请求 : /shoes?user=tom+ferret&order=desc&shoe[color]=blue&shoe[type]=nike

var express = require('express');
var app = express();
app.get('/shoes',function(req,res){
	console.log(req.query.user); //tom ferret
	console.log(req.query.order); // desc
	console.log(req.query.shoe.color); //blue
	console.log(req.query.shoe.type); //nike
	res.json(req.query); // => json obj
});
app.listen(3000);

####req.body

这个对应的是解析过的请求体。这个特性是bodyParser() 中间件提供,新版本的express内部已不存在,需要安装相应的 body-parser插件,其它的请求体解析中间件可以放在这个中间件之后。当bodyParser()中间件使用后,这个对象默认为 {}。

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var urlencodeParser = bodyParser.urlencoded({extended:false});
app.post('/shoes',urlencodeParser,function(req,res){
	console.log(req.body);
	res.json(req.body);
});
app.listen(3000);

####req.files

这是上传的文件的对象。这个特性是bodyParser() 中间件提供,其它的请求体解析中间件可以放在这个中间件之后。当bodyParser()中间件使用后,这个对象默认为 {}。貌似新版的也单独安装中间件,具体不详。

####req.param

返回key参数的值。

var express = require('express');
var app = express();
app.get('/shoes',function(req,res){
	console.log(req.param('user')); //tom ferret
	console.log(req.param('order')); // desc
	res.json(req.query); // => json obj
});
app.listen(3000);

其他形式

// ?name=tobi
req.param('name');// => "tobi"

// POST name=tobi
req.param('name');// => "tobi"

// /user/tobi for /user/:name 
req.param('name');// => "tobi"

####req.route

这个对象里是当前匹配的 Route 里包含的属性,比如原始路径字符串,产生的正则,等等

var express = require('express');
var app = express();
app.get('/user/:id',function myApp(req,res){
	console.log(req.route);
	res.send(req.route);
});
app.listen(3000);

请求访问 localhost:3000/user/10返回为

{
	"path":"user/:id",
	"stack":[
		{
			"name":"myApp",
			"keys":[],
			"regexp":{},
			"method":"get"
		}
	],
	"methods":{
		"get":true
	}
}

####req.cookies

当使用 cookieParser()中间件之后,这个对象默认为{}, 它也包含了用户代理传过来的cookies。

var express      = require('express')
var cookieParser = require('cookie-parser')
var app = express();
app.use(cookieParser());
app.get('/', function(req, res) {
  console.log("Cookies: ", req.cookies)
});
app.listen(3000);

####req.signedCookies

当使用了cookieParser(secret) 中间件后,这个对象默认为{}, 否则包含了用户代理传回来的签名后的cookie,并等待使用。签名后的cookies被放在一个单独的对象里,恶意攻击者可以很简单的替换掉req.cookie 的值。需要注意的是签名的cookie不代表它是隐藏的或者加密的,这个只是简单的阻止篡改cookie。更多使用说明参考cookieParser

####req.get

获取请求头里的值,大小写不敏感. Referer 和 referer 字段是可以互换的。别名为 req.header

var express = require('express');
var app = express();
app.get('/',function(req,res){
	console.log(req.get('User-Agent'));
	console.log(req.header(User-Agent));
	res.send('req.get()获取请求头信息');
});
app.listen(3000);

####req.accepts

检查给定的值是不是可以接受类型,当可以接受时返回最匹配的,否则返回undefined。 参数的值可能是单一的一个mime类型字符串,比如 “application/json”, 扩展名为”json”, 也可以为逗号分隔的列表或者数组。当给定的是数组或者列表,返回最佳匹配的。不明觉厉。

// Accept: text/html
req.accepts('html');
// => "html"

// Accept: text/*, application/json
req.accepts('html');
// => "html"
req.accepts('text/html');
// => "text/html"
req.accepts('json, text');
// => "json"
req.accepts('application/json');
// => "application/json"

// Accept: text/*, application/json
req.accepts('image/png');
req.accepts('png');
// => undefined

// Accept: text/*;q=.5, application/json
req.accepts(['html', 'json']);
req.accepts('html, json');
// => "json"

####req.is

检查请求的文件头是不是包含”Content-Type” 字段, 它匹配给定的值。

// With Content-Type: text/html; charset=utf-8
req.is('html');
req.is('text/html');
req.is('text/*');
// => true

// When Content-Type is application/json
req.is('json');
req.is('application/json');
req.is('application/*');
// => true

req.is('html');
// => false

更多req对象的属性,查看console.log(req)。