express req object

req.params

// get /user/kyfxbl/29

app.get("/user/:id1/:id2", function (req, res) {
    console.log(req.params);// [ id1: 'kyfxbl', id2: '29' ]
    res.end("hello world");
});

req.query

// get /user/kyfxbl/abc?age=29

app.get("/user/:id1/:id2", function (req, res) {
    console.log(req.query);// { age: '29' }
    res.end("the id is: " + req.params.id1);
});

req.body

这个是解析http请求体(post)的结果,需要use middleware(express.bodyParser)


req.param(name)

是一种简写,查找顺序依次是params, body, query


req.route

路由的信息:

app.get("/user/:id1/:id2", function (req, res) {
    console.log(req.route);
    res.end("the id is: " + req.params.id1);
});

GET /user/kyfxbl/29,将会输出:

{ path: '/user/:id1/:id2',
  method: 'get',
  callbacks: [ [Function] ],
  keys: 
   [ { name: 'id1', optional: false },
     { name: 'id2', optional: false } ],
  regexp: /^\/user\/(?:([^\/]+?))\/(?:([^\/]+?))\/?$/i,
  params: [ id1: 'kyfxbl', id2: '29' ] }

req.cookies

cookie信息


req.get()

req.get('User-Agent');// 获取http request header中的信息

req.accepts()

Check if the giventypesare acceptable, returning the best match when true, otherwiseundefined- in which case you should respond with 406 "Not Acceptable".

这个方法比较少用到


req.path

console.log(req.path);

GET /user/kyfxbl/29,返回/user/kyfxbl/29


还有一些特别一目了然的,一起贴了:

req.ip => client(browser) ip

req.host => localhost

req.protocol => http


另外还有一些都是跟http request header相关的查询方法,详见官方reference:express reference

你可能感兴趣的:(express req object)