Found 33676 Articles for Programming

Sending HTTP error code using Express.js

Mayank Agarwal
Updated on 28-Mar-2022 12:51:31

1K+ Views

We can send different HTTP status and responses over the Express.js app endpoint as per the user's requirement. Also we can send a message in case of an error or when the requests are forbidden. The status code 200 is sent by default with the response returned.Syntaxres.status( statusCode )Example 1Create a file with the name "status.js" and copy the following code snippet. After creating the file, use the command "node status.js" to run this code as shown in the example below −// Specifying status code Demo Example // Importing the express module var express = require('express'); // Initializing ... Read More

Skipping middleware in Express.js

Mayank Agarwal
Updated on 28-Mar-2022 14:50:14

940 Views

You need to pass some parameters to skip the middleware in an Express application. On the basis of that parameter with logic in place, you can decide whether to execute a middleware or notSyntaxThere is no syntax defined. You can introduce a parameter and then check based upon that parameter whether to use the middleware or not.ExampleCreate a file with the name "skipMiddleware.js" and copy the following code snippet. After creating the file, use the command "node skipMiddleware.js" to run this code as shown in the example below −// app.set() Demo Example // Importing the express module var express ... Read More

Express.js – res.headersSent Property

Mayank Agarwal
Updated on 28-Mar-2022 12:46:46

1K+ Views

res.headersSent returns a Boolean value that indicates if the app has sent HTTP headers for the response or not. If the headers are sent, it returns True; else False.Syntaxres.headersSentExample 1Create a file with the name "headersSent.js" and copy the following code snippet. After creating the file, use the command "node headersSent.js" to run this code as shown in the example below −// res.headersSent Property 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

Express.js – router.param() Method

Mayank Agarwal
Updated on 28-Mar-2022 14:50:39

1K+ Views

router.param(name, callback) adds a callback to the route parameters where name defines the name of the parameter and callback is the callback function.Following are the parameters of the callback function −req – the request objectres – the response objectnext -- the next middlewarename – value of the name parameterSyntaxrouter.param( name, callback )ExampleCreate a file with the name "routerParam.js" and copy the following code snippet. After creating the file, use the command "node routerParam.js" to run this code as shown in the example below −// router.param() Method Demo Example // Importing the express module var express = require('express'); // Importing ... Read More

Express.js – router.METHOD() Method

Mayank Agarwal
Updated on 28-Mar-2022 12:40:55

162 Views

The router.METHOD() is used for providing the method functionality in Express, where METHOD represents one of the HTTP methods such as GET, POST, PUT, and so on, in lowercase. Therefore, the actual methods are represented as following −router.get()router.post()router.put() …… and so on...Syntaxrouter.METHOD( path, [callback ...], callback )Example 1Create a file with the name "routerMETHOD.js" and copy the following code snippet. After creating the file, use the command "node routerMETHOD.js" to run this code as shown in the example below −// router.METHOD() Method Demo Example // Importing the express module const express = require('express'); // Initializing the express and ... Read More

Express.js – res.get() Method

Mayank Agarwal
Updated on 28-Mar-2022 12:38:19

488 Views

The res.get() method is used to return the HTTP headers specified by the field. The match is case-insensitive and therefore returns all the matching patterns.Syntaxres.get( field )Example 1Create a file with the name "resGet.js" and copy the following code snippet. After creating the file, use the command "node resGet.js" to run this code as shown in the example below −// res.get(field) 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 PORT = 3000; ... Read More

Express.js – req.range() Method

Mayank Agarwal
Updated on 28-Mar-2022 12:35:04

575 Views

req.range() is basically a range header parser. The accept-ranges and the response-header fields allow the server to indicate the acceptance of the range requests from a resource.Syntaxreq.range( size, [options])ParametersThe above parameters are defined as follows −size – The size parameter defines the maximum size of the resource.options – The options parameter can have the following properties −combine – It is a Boolean type variable. This parameter specifies if overlapping and whether the adjacent ranges should be combined or not. Default: FalseExample 1Create a file with the name "reqRange.js" and copy the following code snippet. After creating the file, use the ... Read More

Express.js – req.xhr Property

Mayank Agarwal
Updated on 28-Mar-2022 12:32:20

576 Views

The req.xhr property is a Boolean property that returns True when the request's X-Requested-With header field is "XMLHttpRequest". The True pointer basically indicates that the request was issued by a client library such as jQuery.Syntaxreq.xhrExample 1Create a file with the name "reqXhr.js" and copy the following code snippet. After creating the file, use the command "node reqXhr.js" to run this code as shown in the example below −// req.xhr Property Demo Example // Importing the express module var express = require('express'); // Initializing the express and port number var app = express(); // Initializing the router from ... Read More

Express.js – req.stale Property

Mayank Agarwal
Updated on 28-Mar-2022 12:25:56

216 Views

The req.stale property checks whether a request is fresh or stale in the client's cache. If the property returns True, it means the client's cache is stale and all the data needs to be transferred to the client's system. Else, only the non-cached data needs to be transmitted.Syntaxreq.staleExample 1Create a file with the name "reqStale.js" and copy the following code snippet. After creating the file, use the command "node reqStale.js" to run this code as shown in the example below −// req.stale Property Demo Example // Importing the express var express = require('express'); // Initializing the express and ... Read More

Express.js – req.secure Property

Mayank Agarwal
Updated on 28-Mar-2022 12:13:16

235 Views

The req.secure property returns a Boolean value that returns true if a TLS connection is established, else it will return False.Its logic is similar to the following method −--> req.protocol == "https"Syntaxreq.secureExample 1Create a file with the name "reqSecure.js" and copy the following code snippet. After creating the file, use the command "node reqSecure.js" to run this code as shown in the example below −// req.secure Property Demo Example // Importing the express var express = require('express'); // Initializing the express and port number var app = express(); // Initializing the router from express var router = ... Read More

Advertisements