摘要:對(duì)象表示請(qǐng)求并且具有請(qǐng)求查詢字符串參數(shù)正文標(biāo)題頭等屬性對(duì)應(yīng)用程序?qū)嵗囊帽4媪撕芏鄬?duì)使用中間件的應(yīng)用程序?qū)嵗囊脪燧d在路由實(shí)例上的路徑請(qǐng)求主體和和包含在請(qǐng)求正文中提交的數(shù)據(jù)的鍵值對(duì)默認(rèn)情況下它是未定義的當(dāng)您使用體解析中間件如和時(shí)將被填
2. request
req對(duì)象表示http請(qǐng)求,并且具有請(qǐng)求查詢字符串,參數(shù),正文,http標(biāo)題頭等屬性
app.get("/user/:id", (req, res) => { res.send("user " + req.params.id); });2.1 對(duì)應(yīng)用程序?qū)嵗囊?/b>
req.app()
app保存了很多對(duì)使用中間件的express應(yīng)用程序?qū)嵗囊?/p>
// 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 掛載在路由實(shí)例上的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 請(qǐng)求主體和cookies
req.body和req.cookies
包含在請(qǐng)求正文中提交的數(shù)據(jù)的鍵值對(duì),默認(rèn)情況下,它是未定義的,當(dāng)您使用體解析中間件(如body-parser和multer)時(shí),將被填充
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 主機(jī)信息
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 請(qǐng)求路由
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: "", params: undefined, path: undefined, keys: [], regexp: /^/?$/i, method: "get" } ], methods: { get: true } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/93058.html
摘要:對(duì)象大小寫(xiě)敏感默認(rèn)不敏感保留父路由器的必需參數(shù)值如果父項(xiàng)和子項(xiàng)具有沖突的參數(shù)名稱(chēng)則該子項(xiàng)的值將優(yōu)先激活嚴(yán)格路由默認(rèn)禁用禁用之后正常訪問(wèn)但是不可以訪問(wèn)全部調(diào)用或者或者實(shí)際上就是的各種請(qǐng)求方法使用路由使用模塊方法 Router([options]) let router = express.Router([options]); options對(duì)象 caseSensitive,大小寫(xiě)敏...
1.express() 基于Node.js平臺(tái),快速、開(kāi)放、極簡(jiǎn)的web開(kāi)發(fā)框架。 創(chuàng)建一個(gè)Express應(yīng)用.express()是一個(gè)由express模塊導(dǎo)出的入口top-level函數(shù). const express = require(express); let app = express(); 1.1 靜態(tài)資源管理 express.static(root, [options]) expr...
摘要:如果包含字符則將設(shè)置為。添加字段到不同的響應(yīng)頭將該字段添加到不同的響應(yīng)標(biāo)頭如果它尚未存在。 3. response對(duì)象 3.1 是否發(fā)送了響應(yīng)頭 res.headersSent布爾屬性,app是否發(fā)送了httpheaders const express = require(express); const bodyParser = require(body-parser); cons...
摘要:和的區(qū)別服務(wù)器使用協(xié)議服務(wù)器使用協(xié)議服務(wù)器需要向證書(shū)授權(quán)中心申請(qǐng)證書(shū)一般免費(fèi)證書(shū)何紹需要交費(fèi)在少許讀客戶端有要求的情況下也會(huì)要求客戶端使用證書(shū)服務(wù)器于客戶端之間傳輸?shù)氖敲魑臄?shù)據(jù)而服務(wù)器于客戶端之間傳輸?shù)氖墙?jīng)過(guò)安全加密后的密文數(shù)據(jù)服務(wù)器通常使 4. HTTP和HTTPS的區(qū)別 HTTPS服務(wù)器使用HTTPS協(xié)議,HTTP服務(wù)器使用HTTP協(xié)議. HTTPS服務(wù)器需要向證書(shū)授權(quán)(Ce...
摘要:類(lèi)比一下你有一個(gè)巨型停車(chē)場(chǎng),里邊分了不同的停車(chē)區(qū)集合,這里的,每個(gè)停車(chē)區(qū)可以停很多車(chē)下文提到的,相當(dāng)于每個(gè)數(shù)據(jù)集合里邊可以有很多張數(shù)據(jù)表。 Node.js利用mongoose連接mongodb數(shù)據(jù)庫(kù) Node.js連接mongodb數(shù)據(jù)庫(kù)有很多種方法,通過(guò)mongoose模塊引入是其中的一個(gè)方法 代碼組織結(jié)構(gòu) |---|根目錄 |---|---|connect.js(mongoose測(cè)...
閱讀 2774·2021-11-22 15:11
閱讀 3537·2021-09-28 09:43
閱讀 2889·2019-08-30 13:05
閱讀 3431·2019-08-30 11:18
閱讀 1447·2019-08-29 16:34
閱讀 1300·2019-08-29 13:53
閱讀 2908·2019-08-29 11:03
閱讀 1658·2019-08-29 10:57