Express.js – express.raw() 函式
express.raw() 是 Express 中內建的中介軟體函式。它將傳入的請求解析為緩衝區,基於 body-parser。此方法返回的中介軟體將所有 JSON 主體解析為緩衝區,並且只檢視content-type 頭部與type 選項匹配的請求。
語法
express.raw([options])
引數
以下是此方法可用的不同選項
options –
inflate – 啟用或停用對已解壓縮或壓縮的主體的處理。預設值:true
limit – 控制請求主體的最大大小。
type – 確定將要解析的中介軟體的媒體型別。
示例 1
建立一個名為"expressRaw.js" 的檔案並複製以下程式碼片段。建立檔案後,使用命令"node expressRaw.js"執行此程式碼。
// express.raw() Demo Example
// Importing the express module
var express = require('express');
// Initializing the express and port number
var app = express();
var PORT = 3000;
// Using the express.raw middleware
app.use(express.raw());
// Reading content-type
app.post('/', function (req, res) {
console.log(req.body)
res.end();
})
// Listening to the port
app.listen(PORT, function(err){
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});在訪問 API 端點之前,請設定以下兩個屬性:
在標頭中將content-type 設定為application/octet-stream。
在 POST 請求中傳遞以下主體:– {"name": "TutorialsPoint"}
輸出
C:\home
ode>> node expressRaw.js Server listening on PORT 3000
示例 2
讓我們再來看一個例子。
// express.raw() Demo Example
// Importing the express module
var express = require('express');
// Initializing the express and port number
var app = express();
var PORT = 3000;
// Commenting the express.raw middleware
// app.use(express.raw());
// Reading content-type
app.post('/', function (req, res) {
console.log(req.body)
res.end();
})
// Listening to the port
app.listen(PORT, function(err){
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});在訪問 API 端點之前,請設定以下兩個屬性:
在標頭中將content-type 設定為application/octet-stream。
在 POST 請求中傳遞以下主體:– {"name": "TutorialsPoint"}
輸出
C:\home
ode>> node expressRaw.js Server listening on PORT 3000 undefined
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP