Node.js學習之路25——Express的request物件

Karuru發表於2019-02-16

2. request

req物件表示http請求,並且具有請求查詢字串,引數,正文,http標題頭等屬性

app.get(`/user/:id`, (req, res) => {
    res.send(`user ` + req.params.id);
});

2.1 對應用程式例項的引用

  • req.app()
  • app儲存了很多對使用中介軟體的express應用程式例項的引用
// one.js
module.exports = function (req, res) {
    res.send(`The views directory is ` + req.app.get(`views`))
}
// app.js
app.get(`/one`, require(`./one.js`));

2.2 掛載在路由例項上的URL路徑

  • req.baseUrl
let greet = express.Router();

greet.get(`/one`, (req, res) => {
    console.log(req.baseUrl);// /greet
    res.send(`hello`)
});

app.use(`/greet`, greet);

2.3 請求主體和cookies

  • req.bodyreq.cookies
  • 包含在請求正文中提交的資料的鍵值對,預設情況下,它是未定義的,當您使用體解析中介軟體(如body-parsermulter)時,將被填充
const express = require(`express`);
const bodyParser = require(`body-parser`);
const cookieParser = require(`cookie-parser`)
let app = express();

app.use(bodyParser.json());// parsing application/json
app.use(bodyParser.urlencoded({ extended: true }));// parsing application/x-www-form-urlencoded
app.use(cookieParser())
app.post(`/`, (req, res) => {
    console.log(`Cookies: `, req.cookies);
    console.log(`Signed Cookies: `, req.signedCookies);
    console.log(`req.body`, req.body);
    res.json(req.body);
});

app.post(`/`, (req, res) => {
    console.log(req.body);
    res.json(req.body);
});

app.listen(3000);

2.4 主機資訊

  • fresh,hostname,ip,ips,protocol
const express = require(`express`);
const bodyParser = require(`body-parser`);
const cookieParser = require(`cookie-parser`)
let app = express();

app.use(bodyParser.json());// parsing application/json
app.use(bodyParser.urlencoded({ extended: true }));// parsing application/x-www-form-urlencoded
app.use(cookieParser())

app.get(`/files/download/:user`, (req, res) => {
    console.log(`req.fresh:`, req.fresh);
    console.log(`req.stale:`, req.stale);
    console.log(`req.hostname:`, req.hostname);
    console.log(`req.ip:`, req.ip);
    console.log(`req.ips:`, req.ips);
    console.log(`req.protocol:`, req.protocol);
    console.log(`req.url:`, req.url);
    console.log(`req.originalUrl:`, req.originalUrl);
    console.log(`req.xhr:`, req.xhr);
    console.log(`req.params:`, req.params);
    console.log(`req.path:`, req.path);
    res.send(req.body);
});

app.listen(3000);
/***
request url: http://localhost:1111/files/download/mark
req.fresh: false
req.stale: true
req.hostname: localhost
req.ip: 127.0.0.1
req.ips: []
req.protocol: http
req.url: /files/download/mark// req.url是javascript的http模組的屬性,不是Express的
req.originalUrl: /files/download/mark
req.subdomains: []
req.xhr: false
req.params: { user: `mark` }
req.path: /files/download/mark
***/

2.5 請求路由

  • req.route
app.get(`/user/:id?`, (req, res) => {
    console.log(req.route);
    res.send(`send get message route`)
});
/***
req.route: Route {
  path: `/files/download/:user`,
  stack:
   [ Layer {
       handle: [Function],
       name: `<anonymous>`,
       params: undefined,
       path: undefined,
       keys: [],
       regexp: /^/?$/i,
       method: `get` } ],
  methods: { get: true } }

相關文章