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 app.post to handle POST requests.

For example:

example.com/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.

Example

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

// POST method route
app.post('/', 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 count words in each paragraph

Sometimes you need a number of words for a certain...

  var iTotalWords = $(this).text().split(' ').length;

Target pseudo-class

This pseudo-class is called target pseudo-class and represents an element...

:target {
    outline: red dotted;
}