Express.js - req.range() 方法
req.range() 基本是一個範圍頭解析器。accept-ranges 和 response-header 允許伺服器指示客戶端發起的對某個資源的範圍請求是否可以被接受。
語法
req.range( size, [options])
引數
上面定義的引數如下 −
size – size 引數定義資源的最大大小。
options – options 引數可能具有以下屬性 −
combine – 這是一個布林型變數。該引數指定重疊範圍和相鄰範圍是否應該合併。預設值: False
示例 1
建立一個名為 "reqRange.js" 的檔案並複製以下程式碼段。建立檔案後,使用命令 "node reqRange.js" 來執行此程式碼,如以下示例所示 −
// Specifying status code Demo Example
// Importing the express module
var express = require('express');
// Initializing the express and port number
var app = express();
var PORT = 3000;
// Creating an endpoint
app.get("/api", (req, res) => {
res.status(400);
res.send("Bad Request Received")
})
app.listen(PORT, function(err){
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});訪問以下 URL 端點進行 GET 請求 −
https://:3000/
並設定頭中的以下屬性 −
--> Range(Key) -> bytes=500-1000
輸出
C:\home
ode>> node reqRange.js Server listening on PORT 3000 bytes=500-1000 [ { start: 500, end: 1000 }, type: 'bytes' ]
示例 2(不設定範圍頭)
讓我們看一下另一個示例。
// req.range() code Demo Example
// Importing the express module
var express = require('express');
// Initializing the express and port number
var app = express();
var PORT = 3000;
// Creating an endpoint
app.get('/', function (req, res) {
console.log(req.range());
res.end();
});
app.listen(PORT, function(err){
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});訪問以下 URL 端點進行 GET 請求 − https://:3000/
輸出
C:\home
ode>> node reqRange.js Server listening on PORT 3000 undefined
廣告
資料結構
網路
關係型資料庫
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP