Node JS API Route

Go With The Flow

Routing is shows how an application responds to a client request for a particular endpoint/API, which is a URI (or path) and a specific HTTP request method (GET, POST, and so on).

Each endpoint/route can have one or more handler functions, which are executed when the route is matched. We can write business login inside our handler and return the response to the client.

//Route structure with express
app.<METHOD>(`PATH/ENDPOINT`, HANDLER)

Here are some explanation:
i) `
app` is an Express instance used for running server and initialising routes method.

ii) METHOD is an HTTP REQUEST METHOD, in lowercase.

iii) PATH` is the access point to the endpoint.

iv) HANDLER is a function which handle the api request data and perform some operation and return the response to the client.

//Example of the routes, here function is a route handler
// GET method route
app.get('/', function (req, res) {
 /**
  You can write your business login here
**/
  res.send('This is a get request');
})

// POST method route
app.post('/', function (req, res) {
/**
You can write your business login here
**/
  res.send('This is a post request')
})
// DELETE method route
app.delete('/', function (req, res) {
/**
You can write your business login here
**/
res.send('This is a delete request')
})
// PUT method route
app.put('/', function (req, res) {
/**
You can write your business login here
**/
res.send('This is a put request')
})

There is one special routing method, app.all(), used to load middleware functions at a path for all HTTP request methods. For example, the following handler is executed for requests to the route “/getme” whether using GET, POST, PUT, DELETE, or any other HTTP request method supported in the:

/**
It will applied to all the routes associated with the application
**/
app.all('*', function (req, res, next) {
console.log('Accessign me ...')
next() // pass control to the next handler
})

I hope it will help you guys, Thank you…