English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

NodeJS Basic Tutorial

NodeJS Express.js

NodeJS Buffer&URL;

NodeJS MySql

NodeJS MongoDB

NodeJS File (FS)

NodeJS Other

Express.js Routes

Express.js routing defines how the Express application uses a specific URI (orpath)and a specific HTTP request method (GET, POST, etc.) to respond to client requests.

To understand the requirements of Express.js routing, let's delve into an example.

Create a basic Express application as shown below.

app.js

var express = require('express') 
var app = express() 
 
// Start the server
var server = app.listen(8000, function(){ 
    console.log('Listening on port 8000...') 
 )

What we need to do is, instantiate a quick application, on the port8000 to start it. Now, open the browser and access the URL http://localhost:8000/.

The response is that it cannot obtain the resource " /"

Even if there are no errors in the console, the application runs well.

   

Why is that? Because in the Express application, we started the server, but did not define what must happen when the request arrives at the server.

This is the manifestation of Express.js routing. Below is a simple quick route.

 
 app.get('/', function(req, res) { 
    res.send('This is a basic Example for Express.js by w3codebox') 
 )

What does this route define? When you receive a GET request to the URL, this route defines the execution statement inside the function./.

In the following screenshot, there is a route that executes functionalities for GET requests with the request URL/hello/.

Let's define some routes in app.js and start the server.

app.js

 var express = require('express') 
var app = express() 
 
// for GET requests and request URL path/'or root route
app.get('/', function(req, res) { 
   res.send('Home.') 
 ) 
 
// for GET requests and request URL path/ hello /'Executed route
app.get('/hello/', function(req, res) { 
   res.send('Hello page.') 
 ) 
 
// for GET requests and request URL path/ bye /'Executed route
app.get('/bye/', function(req, res) { 
   res.send('Bye page.') 
 ) 
 
// Start the server
var server = app.listen(8000, function(){ 
    console.log('Listening on port 8000...') 
 )

Start the quick application.

Now click on the URL in the browser. By default, the browser sends a GET request.

with URL path http:// localhost:8000 /for GET requests

with URL path http:// localhost:8000 / hello /for GET requests

with URL path http:// localhost:8000 / bye /for GET requests

Quick route with multiple functionalities

You can provide one or more functionalities in the route. Each functionality is called middleware.

app.js

 var express = require('express') 
var app = express() 
 
// Quick route with multiple functions
app.get('/hello/', function(req, res, next) { 
   res.write('Hello page. ') 
   next() 
 }, function(req, res, next){ 
   res.write('Hello again. ') 
   res.end() 
 ) 
 
// Start the server
var server = app.listen(8000, function(){ 
    console.log('Listening on port 8000...') 
 )

In the browser, the output is

You can also define these functionalities modularly as shown below.

 var express = require('express') 
var app = express() 
 
function hello(req, res, next) { 
   res.write('Hello page. ') 
   next() 
 } 
 
function helloagain(req, res, next){ 
   res.write('Hello again. ') 
   res.end() 
 } 
 
// Quick route with multiple functions
app.get('/hello/', hello, helloagain) 
 
// Start the server
var server = app.listen(8000, function(){ 
    console.log('Listening on port 8000...') 
 )

Summary

In this Express.js tutorial, we learned what an Express.js route is, how to define an Express.js route, and how to use these routes to provide different types of HTTP methods for different URL paths.