Express.js app.get() Request Function
Express.js
Express is a flexible online application framework for Node.js that offers a strong set of functionality for both web and mobile applications. While preserving the familiar and adored Node.js features, Express offers a thin layer of basic web application functionalities. Express is the foundation of many well-known frameworks.
If we analyze the structure of an app, it has a frontend as well as a backend which is based on a server. Whenever a user or client makes some requests to the app, the request is sent to the server and backend, and then the server responds to the corresponding request.
Routing is the response of a client's request by the last node of application.
We use different methods in the Express.js app to respond to the requests made by the client. These methods are based on HTTP protocol, so they are also called HTTP methods.
Generally, there are two most popular methods app.get() and app.post() functions. We always use one or more callback functions with these methods.
In these methods, when there is a request made by the client to the correct endpoint of the app, then these callback functions run.
When there are multiple callback functions, then the next() function is used to transfer the control to the next callback function.
We will provide some examples related to the express.get() function:
For installation, we use:
npm install express
Example:
var express = require('express');
var app = express();
var PORT = 3000;
app.get('/', (req, res) => {
res.send("GET Request Called")
})
app.listen(PORT, function(error){
if (error) console.log("error found ",error);
console.log("Server is working", PORT);
});
For the above code, firstly, we imported the express app in our file and created our variable app.
Now, we make the call to the app.get() function, and there is a callback function. We choose the port number 3000 to display the callback function. So if we go to the localhost at 3000 port, we will see the output as
“Get Request Called”.
Output:
In the path, we can use regular expressions to generalize routes.
We can use the following characters:
?
*
+
()
[]
-
.
For example:
app.get('/pq?rst', (request, response) => {
response.send('? char is used as regular expression’)
})
For the above code, the route path will be either ‘/prst’ or ‘pqrst’ because the ‘?’ character will either remove the character just before it or not. So we have two options.
Example:
app.get('/pq+rst', (request, response) => {
response.send('+ char is used as regular expression')
})
In the above example, the route path can be ‘prst’ or ‘pqrst’ or ‘pqqrst’ or ‘pqqqrst’ and so on.
Basically, the + character is replaced by 0 or more characters to the character just before it.
Example:
app.get('/pq*rst', (request, response) => {
response.send('* char is used as regular expression')
})
In the above example, the route path can be ‘pqrst’ or ‘pqarst’ or ‘pq123rst’ or ‘pqadsffrst’ etc.
Basically ‘*’ character can be replaced by zero or more strings of any length.
So the route path can also be ‘pqJAVATPOINTrst’.
Example:
app.get('/pq(rs)?t', (request, response) => {
response.send('? and () char is used as regular expression')
})
In the above example, there are two possibilities of route path, and that is either ‘pqt’ or ‘pqrst’ because ‘()’ is used collectively for many characters.
So either ‘rs’ will be used or not because of the ‘?’ character.
Example:
app.get(/qrs/, (request, response) => {
response.send('double / is used as regular expression')
})
In the above example, route paths can be ‘pqrst’ or ‘pqrs’ or ‘qrs’ or ‘JAVAqrsTPOINT’ etc. basically, the string between these two ‘/’ should be there in the path, and then that path will be matched.
app.get(/.*rst$/, (request, response) => {
response.send('. And * char is used as regular expression')
})
In the above example, the route path can be ‘pqrst’ pr ‘prst’ or ‘288rst’.Basically, the string which ends with ‘rst’ can be the route path.
Example:
var express = require('express');
var app = express();
var PORT = 3002;
const callBack1 = function (req, res, next) {
console.log('first callback')
next()
}
const callBack2 = function (req, res, next) {
console.log('second callback')
next()
}
const callBack3 = function (req, res) {
res.send('third callback')
}
app.get('/', [callBack1, callBack2, callBack3]);
app.listen(PORT, function(error){
if (error) console.log("error found ",error);
console.log("Server is working", PORT);
});
Output:
In the above example, multiple callback functions are used, and at the end, callback 3 is called, and there is next() used to go to the next callback function.