Express JS Articles

Page 5 of 7

req.fresh Property in Express.js

Mayank Agarwal
Mayank Agarwal
Updated on 29-Jan-2022 362 Views

The req.fresh property returns True or false based upon the status of the client's cache. If the cache is still fresh, True is returned, else False will be returned to indicate that the cache is stale and all the content needs to be sent instead of just non-cached part.When a client sends the Cache-Control as no-cache request header to indicate an end-to-end reload request, False will be returned to make the handling of these requests transparent.Syntaxreq.freshExample 1Create a file with the name "reqFresh.js" and copy the following code snippet. After creating the file, use the command "node reqFresh.js" to run ...

Read More

res.append() Method in Express.js

Mayank Agarwal
Mayank Agarwal
Updated on 29-Jan-2022 1K+ Views

The res.append() method can append a specified value to the HTTP response header field. It creates new headers with the specified values, if it's not already created. The value string can take both a string input or an array.Syntaxres.append( field, [value] )Example 1Create a file with the name "resAppend.js" and copy the following code snippet. After creating the file, use the command "node resAppend.js" to run this code as shown in the example below −// res.append(field, [value]) Method Demo Example // Importing the express module var express = require('express'); // Initializing the express and port number var app ...

Read More

res.app Property in Express.js

Mayank Agarwal
Mayank Agarwal
Updated on 29-Jan-2022 334 Views

The res.app property holds a reference to the instance of an Express Application that is being used by the middleware.Syntaxres.appExample 1Create a file with the name "resApp.js" and copy the following code snippet. After creating the file, use the command "node resApp.js" to run this code as shown in the example below −// res.app 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(res.app.get('views')); ...

Read More

req.path Property in Express.js

Mayank Agarwal
Mayank Agarwal
Updated on 29-Jan-2022 2K+ Views

The req.path property contains the path part of the requested URL. This property is widely used to track the incoming requests and their paths. It is mainly used for logging the requests.Syntaxreq.pathExample 1Create a file with the name "reqPath.js" and copy the following code snippet. After creating the file, use the command "node reqPath.js" to run this code as shown in the example below −// req.path Property Demo Example // Importing the express & cookieParser module var cookieParser = require('cookie-parser'); var express = require('express'); // Initializing the express and port number var app = express(); // Initializing ...

Read More

app.enabled() Method in Express.js

Mayank Agarwal
Mayank Agarwal
Updated on 29-Jan-2022 263 Views

The app.enabled() method checks whether a setting name property is enabled or not. It basically checks the value of a setting name and returns True if the property value is also True.Syntaxapp.enabled( name )Example 1Create a file with the name "appEnabled.js" and copy the following code snippet. After creating the file, use the command "node appEnabled.js" to run this code as shown in the example below −// app.enabled() Method Demo Example // Importing the express module const express = require('express'); // Initializing the express and port number var app = express(); // Initializing the router from express ...

Read More

Using next() function in Express.js

Mayank Agarwal
Mayank Agarwal
Updated on 01-Oct-2021 1K+ Views

Express.js is a powerful tool for building web servers to hit API at backend. It has several advantages which makes it popular, however it has got some drawbacks too, for example, one needs to define different routes or middleware to handle different incoming requests from the client.In this article, we will see how to use the next() function in a middleware of Express.js. There are lots of middleware in Express.js. We will use the app.use() middleware to define the handler of the particular request made by client.Syntaxapp.use(path, (req, res, next) )Parameterspath – This is the path for which the middleware ...

Read More

Express.js – express.text() function

Mayank Agarwal
Mayank Agarwal
Updated on 01-Oct-2021 1K+ Views

express.text() is a built-in middleware function in Express. It parses the incoming request payloads into a string and it is based upon the body-parser. This method returns the middleware that parses all the bodies as strings.Syntaxexpress.text([options])ParametersFollowing are the different options available with this methodoptionsinflate – It enables or disables the handling of the deflated or compressed bodies. Default: truelimit – It controls the maximum size of the request body.defaultCharset – This option specifies the default character set for the text content if the charset is not specified in the Content-type header of the request.type – It determines the media type ...

Read More

Express.js – express.raw() function

Mayank Agarwal
Mayank Agarwal
Updated on 01-Oct-2021 2K+ Views

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.Syntaxexpress.raw([options])ParametersFollowing are the different options available with this methodoptions –inflate – This enables or disables the handling of the deflated or compressed bodies. Default: truelimit – Controls the maximum size of the request body.type – Determines the media type for the middleware that will be parsed.Example 1Create a file with the name "expressRaw.js" ...

Read More

Express.js – express.json() function

Mayank Agarwal
Mayank Agarwal
Updated on 01-Oct-2021 16K+ Views

express.json() is a built-in middleware function in Express. This method is used to parse the incoming requests with JSON payloads and is based upon the bodyparser.This method returns the middleware that only parses JSON and only looks at the requests where the content-type header matches the type option.Syntaxexpress.json([options])ParametersFollowing are the different options available with this methodoptionsinflate – This enables or disables the handling of the deflated or compressed bodies. Default: truelimit – This controls the maximum size of the request body.reviver – This option is passed to the JSON.parse method as the second argument.strict – This enables or disables the ...

Read More

Express.js – app.set() Method

Mayank Agarwal
Mayank Agarwal
Updated on 30-Sep-2021 3K+ Views

The app.set() function assigns or sets a setting name to value. This can store any type of value as the user wants, but there are some certain names that can be used to configure the behaviour of the serverSome of the properties that can be configured with the set functionality are −envetagjsonp escape, etcSyntaxapp.set(name, value)Example 1Create a file with the name "appSet.js" and copy the following code snippet. After creating the file, use the command "node appSet.js" to run this code.// app.set() Demo Example // Importing the express module var express = require('express'); // Initializing the express and ...

Read More
Showing 41–50 of 63 articles
« Prev 1 3 4 5 6 7 Next »
Advertisements