Found 72 Articles for Express JS

Express.js – app.post() Method

Mayank Agarwal
Updated on 30-Sep-2021 13:59:08

6K+ Views

The app.post() method routes all the HTTP POST requests to the specified path with the specified callback functions.Syntaxapp.path(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 with the name "appPost.js" and copy the following code snippet. After creating the file, use the command "node appPost.js" to run this code.// ... Read More

Express.js – app.path() Method

Mayank Agarwal
Updated on 30-Sep-2021 13:55:55

472 Views

The app.path() method returns the canonical path. The path is returned as a string. It is better to use the req.baseUrl method since the app.path() method can be very complicated in complex cases of mounted apps.Syntaxapp.path( )Example 1Create a file with the name "appPath.js" and copy the following code snippet. After creating the file, use the command "node appPath.js" to run this code.// app.path() Demo Example // Importing the express module var express = require('express'); // Initializing the express and port number var app = express(); var PORT = 3000; // Assigning express constructor var app = ... Read More

Express.js – app.param() Method

Mayank Agarwal
Updated on 30-Sep-2021 13:51:22

773 Views

The app.param() method is basically used for adding the callback triggers to the route parameters, where name represents the name of the parameter or an array of them and callback represents the callback function.Syntaxapp.param([name], callback)Parametersname − Represents the name of the parameter or array of parameters as required.callback − Represents the callback function. The parameters of the callback function include the request object, the response object, the next middleware, the value of the parameter, and the name of the parameter in the same order.ExampleCreate a file with the name "appParam.js" and copy the following code snippet. After creating the file, ... Read More

Express.js – app.mountpath Property

Mayank Agarwal
Updated on 30-Sep-2021 13:45:15

312 Views

The app.mountpath property contains those path patterns on which a sub-app was mounted. A sub-app can be defined as an instance of Express that may be used for handling the request to a route.This property is similar to the baseUrl property of the req object; the only difference is that req.baseUrl returns the matched URL path instead of the matched patterns.Syntaxapp.mountpathExample 1Create a file "appMountpath.js" and copy the following code snippet. After creating the file, use the command "node appMountpath" to run this code.// app.mountpath code Demo Example // Importing the express module var express = require('express'); // ... Read More

Express.js – app.locals Property

Mayank Agarwal
Updated on 30-Sep-2021 13:35:14

863 Views

The app.locals object defines the properties that are local variables inside an application. Once the value of app.locals property is set, it persists throughout the life of the application. The res.locals property is valid only for the lifetime of the request.Syntaxapp.localsExample 1Create a file "appLocals.js" and copy the following code snippet. After creating the file, use the command "node appLocals.js" to run this code.// app.locals code Demo Example // Importing the express module var express = require('express'); // Initializing the express and port number var app = express(); // Setting the below email throught out the application ... Read More

Express.js – app.listen() Method

Mayank Agarwal
Updated on 30-Sep-2021 13:29:13

6K+ Views

The app.listen() method binds itself with the specified host and port to bind and listen for any connections. If the port is not defined or 0, an arbitrary unused port will be assigned by the operating system that is mainly used for automated tasks like testing, etc.The app object returned by express() is a JavaScript function, that is passed to Node’s HTTP servers as a callback which handles the requests. This makes the application to provide both HTTP and HTTPS versions of the same app with the same code base, as the app does not inherit from these.Syntaxapp.listen([port], [host], [backlog], ... Read More

Express.js – app.engine() Method

Mayank Agarwal
Updated on 30-Sep-2021 12:37:41

474 Views

The app.engine() method is used for registering the given template engine callback as "ext". The require() method needs the engine based on the function by default.Use the following methods for engines that do not provide the extensions (or want to map different extensions) or express out of the box.app.engine('html', require('ejs').renderFile)Syntaxapp.engine(ext, callback)Example 1Create a file with the name "appEngine.js" and copy the following code snippet. After creating the file, use the command "node appEngine.js" to run this code.// app.engine() Method Demo Example // Importing the express module const express = require('express'); // Initializing the express and port number var ... Read More

Express.js – app.enable() Method

Mayank Agarwal
Updated on 30-Sep-2021 12:16:52

364 Views

The app.enable() function sets the Boolean setting ‘name’ to ‘true’, where name defines one of the properties from the app settings table. Using the app.set('foo', true) for a Boolean property is same as calling the app.enable('foo') function.Syntaxapp.enable(name)Example 1Create a file with the name "appEnable.js" and copy the following code snippet. After creating the file, use the command "node appEnable.js" to run this code.// app.enable() 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 var router = express.Router(); ... Read More

Express.js – app.disable() Method

Mayank Agarwal
Updated on 30-Sep-2021 12:13:31

502 Views

The app.disable() method disables the setting name passed in the function. This method sets the setting name to False. We can perform the same function by using the app.set() method too, by passing its value as False.Syntaxapp.disable(name)Example 1Create a file with the name "appDisable.js" and copy the following code snippet. After creating the file, use the command "node appDisable.js" to run this code.// app.disable() 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 var router = express.Router(); ... Read More

Express.js – app.delete() Method

Mayank Agarwal
Updated on 30-Sep-2021 12:09:47

5K+ Views

The app.delete() method routes all the HTTP DELETE requests to the specified path with the specified callback functions.Syntaxapp.delete(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 act like a middleware except that these callbacks can invoke next (route).Example 1Create a file "appDelete.js" and copy the following code snippet. After creating the file, use the command "node appDelete.js" to run this code.// app.delete() Method Demo ... Read More

Advertisements