Programming Articles - Page 632 of 3366

Express.js – req.baseUrl Property

Mayank Agarwal
Updated on 06-Apr-2022 08:13:49

2K+ Views

The req.baseUrl property returns the router instance where this URL path is mounted. This property is similar to the mountpath property of the app object, except for the difference that app.mountpath returns the matched path patterns.Syntaxreq.baseUrlExample 1Create a file with the name "reqBaseUrl.js" and copy the following code snippet. After creating the file, use the command "node reqBaseUrl.js" to run this code as shown in the example below −// req.baseUrl Property Demo Example // Importing the express var express = require('express'); // Initializing the express and port number var app = express(); // Initializing the router from express ... Read More

Express.js – res.end() Method

Mayank Agarwal
Updated on 06-Apr-2022 08:09:42

4K+ Views

The res.end() method ends the current response process. This method is used to quickly end the response without any data. If one needs to respond with data, they should use either the res.send() method or the res.json() method.Syntaxres.end([data], [encoding])Default encoding is 'utf-8'.Example 1Create a file with the name "resEnd.js" and copy the following code snippet. After creating the file, use the command "node resend.js" to run this code as shown in the example below −// res.end() Method Demo Example // Importing the express module var express = require('express'); // Initializing the express and port number var app = express(); ... Read More

Generating Random Short Id in Node.js

Mayank Agarwal
Updated on 06-Apr-2022 08:07:04

1K+ Views

The 'shortId' package from NPM can be used to create short non-sequential URL-friendly unique ID's. By default, it returns a 7-14 URL-friendly characters from the following categories: "A-Z, a-z, 0-9, _, -". This package also supports clusters (automatically), custom seeds, and custom alphabets. It can generate any number of ID's without duplication.SyntaxSetting up the NPM project:npm init -yInstalling the 'shortId' dependency:npm install express shortidImporting shortId:const short = require('shortid');Example 1Create a file with the name "shortId.js" and copy the following code snippet. After creating the file, use the command "node shortId.js" to run this code as shown in the example below ... Read More

Getting the Domain Info for Request in Express.js

Mayank Agarwal
Updated on 06-Apr-2022 12:47:07

3K+ Views

We need to get the DNS information to track the address from where we receive the requests. This feature also provides an added layer of security, protecting the application from different type of DOS and DDOS attacks.We can use the following functions to get the domain and host information.SyntaxGetting the origin info:var origin = req.get('origin');Getting the host info:var host = req.get('host');Example 1Create a file with the name "dnsInfo.js" and copy the following code snippet. After creating the file, use the command "node dnsInfo.js" to run this code as shown in the example below −// Getting the Host info Demo Example ... Read More

Getting Query String Variables in Express.js

Mayank Agarwal
Updated on 06-Apr-2022 07:54:55

10K+ Views

In Express.js, you can directly use the req.query() method to access the string variables. As per the documentation, the req.param method only gets the route parameters, whereas the req.query method checks the query string parameters. For example, "?id=12" checks urlencoded body params.Syntaxreq.query( )Example 1Create a file with the name "reqQuery.js" and copy the following code snippet. After creating the file, use the command "node reqQuery.js" to run this code as shown in the example below −// req.query() Demo Example // Importing the express module var express = require('express'); // Initializing the express and port number var app = express(); ... Read More

Express.js – req.acceptsLanguage() Method

Mayank Agarwal
Updated on 06-Apr-2022 07:49:47

615 Views

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

224 Views

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

993 Views

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

7K+ Views

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

C++ code to find different winner and non-winner counts on a contest

Arnab Chakraborty
Updated on 30-Mar-2022 14:29:04

272 Views

Suppose we have two numbers n and k, There are n students in a contest. Some of them will receive memento, some will get certificates, and others won't receive anything. Who receives something are called winners. But there are some rules of counting the number of memento and certificates. The number of certificates must be exactly k times greater than the number of memento. The number of winners must not be greater than n/2. It's also possible that there are no winners. We have to identify the maximum possible number of winners, according to these rules. Then find the number ... Read More

Advertisements