Express.js req.range() Function
Last Updated :
17 Mar, 2023
The req.range() function is basically a Range header parser where the Accept-Ranges response-header field allows the server to indicate its acceptance of range requests for a resource.
Syntax:
req.range(size[, options])
Parameter:
- size: It is the maximum size of the resource.
- options: It is an object which can have combine property.
Return Value: It returns an array of ranges.
Installation of the express module:
You can visit the link to Install the express module. You can install this package by using this command.
npm install express
After installing the express module, you can check your express version in the command prompt using the command.
npm version express
After that, you can just create a folder and add a file, for example, index.js. To run this file you need to run the following command.
node index.js
Project Structure:
Example 1: Filename: index.js
javascript
const express = require('express');
const app = express();
const PORT = 3000;
app.get('/', function (req, res) {
console.log(req.get('Range'));
console.log(req.range());
res.end();
});
app.listen(PORT, function (err) {
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});
Steps to run the program:
Make sure you have installed the express module using the following command:
npm install express
Run the index.js file using the below command:
node index.js
Output:
Console Output:
Server listening on PORT 3000
Browser Output:
Now make a GET request to http://localhost:3000/ with header set to ' Range: bytes=200-1000', then you will see the following output on your console:
Server listening on PORT 3000
bytes=200-1000
[ { start: 200, end: 1000 }, type: 'bytes' ]
Example 2: Filename: index.js
javascript
const express = require('express');
const app = express();
const PORT = 3000;
app.get('/', function (req, res) {
// Without range header set
console.log(req.range());
res.end();
});
app.listen(PORT, function (err) {
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});
Steps to run the program:
Run the index.js file using the below command:
node index.js
Now make a GET request to http://localhost:3000/ without setting any header, then you will see the following output on your console:
Output:
Server listening on PORT 3000
undefined
Reference: https://expressjs.com/en/5x/api.html#req.range
Similar Reads
Express.js req.param() Function The req.param() function basically returns the value of the param name when present. When parameters are passed, then it can be accessed using this function. Syntax:Â req.param(name [, defaultValue]) Parameter: The name parameter is the name of the parameter. Return Value: String. Installation of th
2 min read
Express.js router.param() function The parameters of router.param() are a name and function. Where the name is the actual name of the parameter and the function is the callback function. Basically, the router.param() function triggers the callback function whenever the user routes to the parameter. This callback function will be call
2 min read
Express res.render() Function âThe res.render() function in Express.js is used to render a view template and send the resulting HTML to the client. This allows you to dynamically generate HTML pages by combining your templates with data.Syntax: res.render(view [, locals] [, callback]);Parametersview (required): A string represen
4 min read
Express.js req.get() Function The req.get() function returns the specified HTTP request header field which is a case-insensitive match and the Referrer and Referrer fields are interchangeable. Syntax:req.get( field )Parameter: The field parameter specifies the HTTP request header field. Return Value: String. Installation of the
2 min read
Express res.send() Function The res.send function is used to send a response in form of various types of data, such as strings, objects, arrays, or buffers, and automatically sets the appropriate Content-Type header based on the data type.res.send in ExpressThe res.send() function sends the HTTP response. The body parameter ca
3 min read