Routing in node js express app

Share

Read Previous: https://codewithsudeep.com/sudeep/javascript/backend-development-part-2-express-app

This article is a part of the backend development series. In the previous article, we created a basic express app with few routes.

In this article, we will dig deeper into routing in the express app and parameters in routes.

** For testing, I recommend using the thunder client or postman **

Basic Routing

Routing is the process of choosing an application’s response to a client request for a certain endpoint, which is a URI (or route) and a particular HTTP request type (GET, POST, and so on). When a route is matched, one or more handler functions may be called for that route.

GET Method

// GET method route
app.get('/', (req, res) => {
  res.send('GET request to the homepage')
})

POST Method

// POST method route
app.post('/', (req, res) => {
  res.send('POST request to the homepage')
})

PUT Method

app.put('/user-detail', (req, res) => {
  res.send('PUT request at /user-detail')
})

DELETE Method

app.delete('/user-id', (req, res) => {
  res.send('DELETE request at /user-id')
})

For all HTTP request methods, the custom routing method app.all() is used to load middleware routines at a path. For instance, the handler listed below is used for requests to the route "/magic" regardless of whether they are made using the GET, POST, PUT, DELETE, or any other HTTP request method that the http module supports.

app.all('/magic', (req, res, next) => {
  console.log('magic section ...')
})

Route parameters

The values supplied at their place in the URL are captured by route parameters, also known as URL segments. The names of the route parameters supplied in the path are used as the keys for the captured values in the req.params object.

Let’s consider a URL with parameters in the route which is responsible to send user details of a specific user in response.

http://localhost:3000/user/1232244234

Raw structure of URL:

http://localhost:3000/user/userId

app.get('/user/:userId', (req, res) => {
  const userdetails =  `Details of user having user id: ${req.params.userId}`
  res.send(userdetails)
})

We can pass multiple parameters in a route. For example, we have a route:

http://localhost:3000/user/:userId/posts/:postID

Here we have assigned two parameters in the route userId and postId

app.get('/user/:userId/post/:postId', (req, res) => {
  const details =  `User id: ${req.params.userId} \n Post ID: ${req.params.postId}`
  res.send(details)
})

Complete Code:

const express = require('express')
const app = express()

// GET method route
app.get('/', (req, res) => {
  res.send('GET request to the homepage')
})

// POST method route
app.post('/', (req, res) => {
  res.send('POST request to the homepage')
})

app.put('/user-detail', (req, res) => {
  res.send('PUT request at /user-detail')
})

app.delete('/user-id', (req, res) => {
  res.send('DELETE request at /user-id')
})

app.all('/magic', (req, res, next) => {
  console.log('magic section ...')
})

app.get('/user/:userId', (req, res) => {
  const userdetails =  `Details of user having user id: ${req.params.userId}`
  res.send(userdetails)
})

app.get('/user/:userId/post/:postId', (req, res) => {
  const details =  `User id: ${req.params.userId} \n Post ID: ${req.params.postId}`
  res.send(details)
})

app.listen(3000, () => {
  console.log(`+ Express App Started\n+ Port:${3000}`)
})

Later in this series, we will discuss route handlers and middleware.

Share
Sudeep Mishra

Sudeep Mishra

Healing

%d bloggers like this: