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)

Other NodeJS

Express.js Tutorial

Welcome to the Express.js tutorial. In this series of Express.js tutorials, we will learn how to start using Express.js and different concepts of Express.js through detailed examples.

Express.js Getting Started

The following two tutorials provide an in-depth introduction to the Express.js web framework and installation.

  • What is Express.js? – A brief introduction to Express.js.

  • Install Express.js – Steps to install express.js using npm.

Express.js Example

The following is a simple example of an Express.js application.

var express = require('express') 
 
// Create a quick application instance
var app = express() 
 
// Quick Routes
app.get('/', function(req, res) { 
   res.send('This is a basic Example for Express.js by w3codebox') 
 }) 
 
// Start the server
var server = app.listen(8000)

In the above code, we created an instance of the express application and then defined a router to handle requests on the GET URL path/Then, we start the server to listen on port 8000.

More detailed examples of building and running web applications are provided at the following location:Express.js Tutorial– Express.js Example Application.

Express.js Routes

Express.js routes are those that handle specific HTTP requests at specified URL paths. Here is an example of Express routes.

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

app is a fast application instance. We can call HTTP methods such as GET (as shown in the above code snippet), POST, HEAD, COPY, PATCH, MOVE, etc. The first parameter is the URL path. The function (the second parameter of the route) is hooked to the path that matches the specified path. In the above example, the function(req, res) is only hooked to those requests with the baseurl path/hello/.

Express.js Middleware

Middleware is a function that can be executed in the order of the request before the response is sent to the client. Here is an example.

var express = require('express') 
var app = express() 
 
// Define middleware function
function logger(req, res, next) { 
   console.log(new Date(), req.url) 
   next() 
 } 
 
// At each request-Call logger:middleware in the response cycle
app.use(logger)

A logger is a middleware feature that can take requests and responses as parameters. next() can also be called in the request-Continue other functions in the response cycle.

A complete Express.js tutorial on middleware – Express.js Middleware.

Express.js Router

Express Router is used to create independent Router objects.