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

Awesome reponsive table

Recently I had one custom CRM project where I had...

.responsive-table tbody td[data-title]:before {
    content: none;

Short-circuits conditionals

If you have to execute a function only if condition...

// You can use short-circuit
condition && doSomething();