9951 explained code solutions for 126 technologies


expressjsHow can I use Express.js to create a next function?


Express.js is a web application framework for Node.js that simplifies the process of creating server-side applications. It can be used to create a next function, which is a function that is called at the end of a request-response cycle.

Example code

const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.send('Hello World!');
});

app.use((req, res, next) => {
  console.log('This is the next function!');
  next();
});

app.listen(3000, () => console.log('Server started on port 3000'));

Output example

Server started on port 3000

The code consists of the following parts:

  1. const express = require('express'); - This imports the Express module.
  2. const app = express(); - This creates an Express application.
  3. app.get('/', (req, res) => { res.send('Hello World!'); }); - This creates a route handler for the root path that sends a response with the text “Hello World!”.
  4. app.use((req, res, next) => { console.log('This is the next function!'); next(); }); - This creates a middleware function that logs a message and then calls the next() function to continue the request-response cycle.
  5. app.listen(3000, () => console.log('Server started on port 3000')); - This starts the server on port 3000.

Helpful links

Edit this code on GitHub