Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Express.js – express.raw() function
express.raw() is a built-in middleware function in Express. It parses the incoming requests into a buffer and it is based upon the body-parser. This method returns the middleware that parses all JSON bodies as buffer and only looks at the requests where the content-type header matches the type option.
Syntax
express.raw([options])
Parameters
Following are the different options available with this method
-
options –
inflate – This enables or disables the handling of the deflated or compressed bodies. Default: true
limit – Controls the maximum size of the request body.
type – Determines the media type for the middleware that will be parsed.
Example 1
Create a file with the name "expressRaw.js" and copy the following code snippet. After creating the file, use the command "node expressRaw.js" to run this code.
// 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);
});
Before hitting the API endpoint, set the following two properties −
Set the content-type as application/octet-stream in headers.
Pass the following body in the POST request – {"name": "TutorialsPoint"}
Output
C:\home\node>> node expressRaw.js Server listening on PORT 3000
Example 2
Let’s take a look at one more example.
// 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);
});
Before hitting the API endpoint, set the following two properties −
Set the content-type as application/octet-stream in headers.
Pass the following body in the POST request – {"name": "TutorialsPoint"}
Output
C:\home\node>> node expressRaw.js Server listening on PORT 3000 undefined
