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