Found 70 Questions for Express JS

req.hostname Property in Express.js

Mayank Agarwal
Updated on 29-Jan-2022 07:39:19
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
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
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
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
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
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
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
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
Updated on 29-Jan-2022 07:11:22
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

Express.js – app.METHOD() in Method

Mayank Agarwal
Updated on 01-Oct-2021 07:23:47
app.METHOD() is used for mapping or routing an HTTP request where METHOD represents the HTTP method of the request such as GET, POST, PUT, etc., but in lowercase. Therefore, the methods are app.get(), app.post(), app.get(), and so on.Syntaxapp.METHOD(path, callback, [callback])Parameterspath − This is the path for which the middleware function is invoked. A path can be a string, path pattern, a regular expression or an array of all these.callback − These are the middleware functions or a series of middleware functions that acts like a middleware except that these callbacks can invoke next (route).Example 1Create a file "appMethod.js" and copy ... Read More
Advertisements