Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Express.js – app.all() Method
The app.all() method can be used for all types of routings of a HTTP request, i.e., for POST, GET, PUT, DELETE, etc., requests that are made to any specific route. It can map app types of requests with the only condition that the route should match.
Syntax
app.path(path, callback, [callback])
Parameters
- path − 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 1
Create a file with the name "appAll.js" and copy the following code snippet. After creating the file use the command "node appal.js" to run this code.
// app.all() 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();
var PORT = 3000;
app.all('/api', function (req, res, next) {
console.log('API CALLED');
next();
});
app.listen(PORT, function(err){
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});
Hit any of the following Endpoints and the response will still be the same.
- POST – http://localhost:3000/api
- GET –http://localhost:3000/api
- DELETE – http://localhost:3000/api
- PUT – http://localhost:3000/api
Output
C:\home\node>> node appAll.js Server listening on PORT 3000 API CALLED
Example 2
Let’s take a look at one more example.
// app.all() 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();
var PORT = 3000;
app.all('/api', function (req, res, next) {
console.log('/api called with method: ', req.method);
next();
});
app.listen(PORT, function(err){
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});
Hit the following Endpoints in sequence to get the response
- POST – http://localhost:3000/api
- PUT – http://localhost:3000/api
- GET – http://localhost:3000/api
Output
C:\home\node>> node appPost.js Server listening on PORT 3000 /api called with method: POST /api called with method: PUT /api called with method: GET
Advertisements
