Request 对象实例讲解
request是客户请求的抽象,这个对象是只读的,包含了客户请求的信息。由于API很简单,以下内容基本上是Express官网API的中文翻译,特殊的地方会做解释说明。
req.params
// /user/id001
router.get(“/user/:id”,function(req,res){
var userId = req.params.id; // id001
})this也可通过req.params[0] 获得id参数。look
req.query
// /user?id=id001&name=leo
router.get(“/user”,function(req,res){
var userId = req.query.id; // id001
var name = req.query.name // leo
})req.param(name)
这是个方法,和params作用一样。
// /user/id001
router.get(“/user/:id”,function(req,res){
// var userId = req.params.id; // id001
var userId = req.param(“id”); // id001
})req.route
获得路由信息
app.get('/user/:id?', function(req, res){
console.log(req.route);
});通过 /user/12 输出结果为:
{ path: '/user/:id?',
keys: [ { name: 'id', optional: true } ],
regexp: /^\/user(?:\/([^\/]+?))?\/?$/i,
params: [ id: '12' ] }这些信息在特定时,会很有用途。
req.cookies
req.cookies.name // 访问cookies信息
req.signedCookies
获得签名cookies信息,当我们在cookieParser(secret) 插件应用 secret时,cookie是经过签名的。
// 真实cookie值是 user=leo.CP7AWaXDfAKIRfH49dQzKJx7sKzzSoPq7/AcBBRVwlI3 req.signedCookies.user // 可以得到抛弃签名后的cookie值。 // => "leo"
req.get(field)
得到请求头部信息,field不区分大小写。
req.get('Content-Type');
// => "text/plain"
req.get('content-type');
// => "text/plain"它的别名是 req.header(field)this
req.accepts(types)
检查给定的类型是可以接受的。
// 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 不允许的类型,将返回undefined
// Accept: text/*;q=.5, application/json
req.accepts(['html', 'json']);
req.accepts('html, json');
// => "json"req.acceptsCharset(charset)
检查给定的字符集是可以接受的。
req.acceptsLanguage(lang)
检查给定语言是可以接受的。
req.is(type)
检查请求对象的内容类型,内部根据Context-Type判断。
// 如果请求头部信息是 Content-Type: text/html; charset=utf-8
req.is('html');
req.is('text/html');
req.is('text/*');
// => true
// 如果请求头部信息是 Content-Type is application/json
req.is('json');
req.is('application/json');
req.is('application/*');
// => true
req.is('html');
// => falsereq.ip
客户ip地址
req.ips
返回代理ip和客户端ip,[“客户ip”,”代理ip2“,”代理ip1”]
req.path
返回请求路径。
// /users?sort=desc req.path // => "/users"
req.host
返回主机名
// Host: "localhost:3000" req.host // => "localhost"
// Host: "127.0.0.1:3000" req.host // => "127.0.0.1"
req.fresh
测试请求req是否新鲜,true/false ,如果客户端没有这个资源的缓冲,那么就表示这个请求的新鲜的。 如果 fresh = false,这时我们可调用 res.status(304) ,这样客户端会直接应用缓冲内容,节省服务器开销。
req.stale
与req.fresh相反,测试请求req是否是陈旧的,true/false
req.xhr
请求是否是ajax方式,true/false
req.protocol
请求协议,http / https
req.secure
是否为安全协议 req.protocol === “https”
req.subdomains
得到子域名数组。 // Host: "a.book.jsera.net" req.subdomains // => ["book", "a"]
req.originalUrl
得到完整请求地址
/user/leo/id001
req.url
这里的url是路由本身域内的,看下面例子:
访问 /user/leo/id001
var router = express.Router();
router.get("/:name/:id",function(req,res){
console.log(req.originalUrl); // /user/leo/id001
console.log(req.url); // /leo/id001
})
app.use("/user",router);