Found 70 Questions for Express JS

Express.js – req.acceptsLanguage() Method

Mayank Agarwal
Updated on 06-Apr-2022 07:49:47
The req.acceptsLanguage() method returns the first accepted language out of the specified languages based on the request's Accept-Language HTTP header field. This method returns 'false' if none of the specified language is accepted.Syntaxreq.acceptsLanguage ( lang, [...] )Example 1Create a file with the name "reqLang.js" and copy the following code snippet. After creating the file, use the command "node reqLang.js" to run this code as shown in the example below −// req.acceptsLanguage(lang, [...]) Method Demo Example // Importing the express module var express = require('express'); // Initializing the express and port number var app = express(); // Initializing the ... Read More

Express.js – req.acceptsCharsets() Method

Mayank Agarwal
Updated on 06-Apr-2022 07:43:35
The req.acceptsCharsets() method returns the first accepted charset of the specified charset sets. These charsets are based on the request's Accept-Charset HTTP header field. By default, it returns 'false' if none of the specified charsets is accepted.Syntaxreq.acceptsCharsets ( charset, [...] )Example 1Create a file with the name "reqAcceptsCharsets.js" and copy the following code snippet. After creating the file, use the command "node reqAcceptsCharsets.js" to run this code as shown in the example below −// res.acceptsCharsets(lang, [...]) Method Demo Example // Importing the express module var express = require('express'); // Initializing the express and port number var app = express(); ... Read More

Express.js – req.accepts() Method

Mayank Agarwal
Updated on 06-Apr-2022 07:38:54
The req.accepts() method checks if the specified content-types are acceptable by the request's Accept HTTP header fields. This method returns the best match, and returns False if none of the specified content types is acceptable.The type values can be a MIME type like application/json, or an extension name like json.Syntaxreq.accepts( types )Example 1Create a file with the name "reqAccepts.js" and copy the following code snippet. After creating the file, use the command "node reqAccepts.js" to run this code as shown in the example below −// req.accepts() Method Demo Example // Importing the express module var express = require('express'); // ... Read More

Express.js – express.urlencoded() Method

Mayank Agarwal
Updated on 06-Apr-2022 07:33:40
express.urlencoded() is a built-in middleware in Express.js. The main objective of this method is to parse the incoming request with urlencoded payloads and is based upon the body-parser.This method returns the middleware that parses all the urlencoded bodies.Syntaxexpress.urlencoded( [options] )ParametersFollowing are the different options available with this method −options −inflate − This enables or disables the handling of the deflated or compressed bodies. Default: truelimit − This controls the maximum size of the request body.extended − This option allows to choose between parsing the URL encoded data with the queryString Library or the qs Library.type − This determines the media ... Read More

Sending HTTP error code using Express.js

Mayank Agarwal
Updated on 28-Mar-2022 12:51:31
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
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
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
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
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
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
Advertisements