In the previous chapter (Express JS Routing) you learn that Route handler is the function executed when the route is matched.
Now in this chapter you’ll learn more about Expresss JS route handler.
Route Handler
The first thing you need to Note that, each route may have one or more handler functions, which are executed when matching the route.
The route handler accepts four parameters. But in here we are talking about three parameters (request, response and next)
All the parameters list of route handler –
- error (err)
- request (req)
- response (res)
- next
const express = require('express');
const app = express();
// route
app.get('/', (req, res, next) => {
res.send('Hello Express.')
});
app.listen(3000, () => console.log('Your app listening on port 3000'));
request (req):
You can handle the clients’ requests by using the request object properties.
Click here to learn more about request object properties and methods.
1 – Example
http://localhost:3000/?name=w3jar.com
Accessing then name parameter by using the query property
const express = require('express');
const app = express();
// route
app.get('/', (req, res) => {
// getting the name parameter
let name = req.query.name;
// if the name is undefined then set the name is empty
if(!name) name = 'empty';
res.send(name);
});
app.listen(3000, () => console.log('Your app listening on port 3000'));
2 – Example
http://localhost:3000/post/25
Accessing the post id by using the request object params property
const express = require('express');
const app = express();
// route
app.get('/post/:id', (req, res) => {
// getting the post id
let id = req.params.id;
// if id is undefined then set id is 0
if(!id) id = '0';
res.send(`Post ID is ${id}`);
});
app.listen(3000, () => console.log('Your app listening on port 3000'));
response (res):
The response object is used to send the server response to the client (according to the route or HTTP request).
Click here to learn more about response object properties and methods.
1 – Example
const express = require('express');
const app = express();
// Home page
app.get('/', (req, res) => {
res.send('This is Home page');
});
// About page
app.get('/about', (req, res) => {
res.send('This is About page');
});
// Contact page
app.get('/contact', (req, res) => {
res.send('This is Contact us page');
});
app.listen(3000, () => console.log('Your app listening on port 3000'));
http://localhost:3000/
http://localhost:3000/about
http://localhost:3000/contact
2 – Example
Set header
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.set('Content-Type', 'text/plain');
res.send('<h1>Hello World!</h1>');
});
app.listen(3000, () => console.log('Your app listening on port 3000'));
next:
next() is used to pass control to the next middleware function.
Note: Each route may have one or more handler functions.
const express = require('express');
const app = express();
app.get('/', (req, res, next) => {
let password = req.query.password;
// if password is equal to 123 then call next();
if(password == 123) next();
res.send('Access denied');
},
(req, res) => {
res.send('Hi Express');
}
);
app.listen(3000, () => console.log('Your app listening on port 3000'));
http://localhost:3000/?password=256
http://localhost:3000/?password=123
Another Example
app.use() is used to applying middleware
const express = require('express');
const app = express();
app.use((req, res, next) => {
let password = req.query.password;
// if password is equal to 123 then call next();
if(password == 123) next();
res.send('Access denied');
});
app.get('/', (req, res) => {
res.send('Hi Express');
});
app.listen(3000, () => console.log('Your app listening on port 3000'));
Both examples are doing the same thing.
Next is – Express JS Router
Previous is – Express JS Routing