Routing in Node.js with Express.Js

In this post I will try to explain how routing works in Express.Js.  Routing refers to how an application’s endpoints (URIs) respond to client requests.  You define routing using methods of the Express app object that correspond to HTTP methods; for example, app.get() to handle GET requests and to handle POST requests.

For example:

Let’s build our first simple route.

var express = require('express')
var app = express()

// respond with "hello from me" when a GET request is made to the homepage
app.get('/', function (req, res) {
  res.send('hello from me')

This function takes two parameters, req (request) and res (response). Using the response object and we render our homepage.

Route methods

A route method is derived from one of the HTTP methods, and is attached to an instance of the express class.


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

// POST method route'/', function (req, res) {
  res.send('POST request to the homepage')

The following code is an example of routes that are defined for the GET and the POST methods to the root of the app.

You can read more about routes here.

You can also read this

How to apply single quotes around every value using VS code?

I often work with large documents and very often I...


Remove duplicate elements from list by value with jQuery

jQuery is very useful and easy to learn and it’s...

.list {