Found 72 Articles for Express JS

router.use() Method in Express.js

Mayank Agarwal
Updated on 29-Jan-2022 07:45:17

1K+ Views

The router.use() method has an optional mount path which is set to "/" by default. This method uses the specified middleware functions for this optional mountpath. The method is similar to app.use().Syntaxrouter.use( [path], [function, ...] callback )Example 1Create a file with the name "routerUse.js" and copy the following code snippet. After creating the file, use the command "node routerUse.js" to run this code as shown in the example below:// router.use() Method Demo Example // Importing the express module const express = require('express'); // Initializing the express and port number var app = express(); // Initializing the router ... Read More

res.vary() Method in Express.js

Mayank Agarwal
Updated on 29-Jan-2022 07:42:21

208 Views

The res.vary() method can be used for adding the field to the Vary response header, if the field does not already exist there. The vary header is basically used for content negotiation.Syntaxres.vary( field )Example 1Create a file with the name "resVary.js" and copy the following code snippet. After creating the file, use the command "node resVary.js" to run this code as shown in the example below −// res.vary() Method Demo Example // Importing the express module var express = require('express'); // Initializing the express and port number var app = express(); // Initializing the router from express ... Read More

req.hostname Property in Express.js

Mayank Agarwal
Updated on 29-Jan-2022 07:39:19

1K+ Views

The req.hostname contains the hostname that is derived from the host HTTP header. This property will get its value from the X-Forwarded-Host header field when the trust setting properties are enabled (or not set to false). This header can be set by the client or by the proxy.The value of the first header is used if more than one X-Forwarded-Host headers are there in the request.Syntaxreq.hostnameExample 1Create a file with the name "reqHostname.js" and copy the following code snippet. After creating the file, use the command "node reqHostname.js" to run this code as shown in the example below −// req.hostname ... Read More

res.cookie() Method in Express.js

Mayank Agarwal
Updated on 29-Jan-2022 07:35:09

6K+ Views

The res.cookie() method is used for setting the cookie name to value. The value parameter can be a string or an object converted to JSON.Syntaxres.cookie( name, value, [options] )ParametersThe options parameter can have the following values −domain − It represents the domain name of the cookie. Default refers to the domain name of the app.encode − This parameter is used for encoding the cookie values in an asynchronous function.expires − This parameter defines the expiry time of the cookie in GMT format. Default value is 0, that creates a session cookie.httpOnly − This Boolean parameter flags the cookie to be ... Read More

req.fresh Property in Express.js

Mayank Agarwal
Updated on 29-Jan-2022 07:32:45

230 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

router.all() Method in Express.js

Mayank Agarwal
Updated on 29-Jan-2022 07:30:53

369 Views

The router.all() method matches all the HTTP Methods. This method is mainly used for mapping "global" logic for specific path prefixes and arbitrary matches.Syntaxrouter.all( path, [callback, ...] callback )Example 1Create a file with the name "routerAll.js" and copy the following code snippet. After creating the file, use the command "node routerAll.js" to run this code as shown in the example below −// router.all() Method Demo Example // Importing the express module var express = require('express'); // Initializing the express and port number var app = express(); // Initializing the router from express var router = express.Router(); var ... Read More

req.cookies Property in Express.js

Mayank Agarwal
Updated on 29-Jan-2022 07:25:51

1K+ Views

The req.cookies contains the cookies sent by the request while using the cookie-parser middleware. If the cookies are signed, please use the req.signedCookies property.Syntaxreq.cookiesInstall the cookie-parser module −npm install cookie-parserExample 1Create a file with the name "reqCookies.js" and copy the following code snippet. After creating the file, use the command "node reqCookies.js" to run this code as shown in the example below −// req.cookies() Method 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 the router ... Read More

res.append() Method in Express.js

Mayank Agarwal
Updated on 29-Jan-2022 07:22:30

941 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
Updated on 29-Jan-2022 07:17:58

181 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
Updated on 29-Jan-2022 07:15:56

1K+ 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

Advertisements