Express JS route handler

Express JS route handler

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'));
Browser Output
w3jar.com

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'));
Browser Output
Post ID is 25

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/

Browser Output
This is Home page

http://localhost:3000/about

Browser Output
This is About page

http://localhost:3000/contact

Browser Output
This is Contact us page

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'));
Browser Output
<h1>Hello World!</h1>

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

Browser Output
Access denied

http://localhost:3000/?password=123

Browser Output
Hi Express

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

Posts Navigation


Leave a Reply

Your email address will not be published. Required fields are marked *