9951 explained code solutions for 126 technologies


expressjsHow can I use Express.js to develop a web application?


Express.js is a web application framework for Node.js, which is used to create web applications. It provides a robust set of features for web development, such as routing, middleware, view-engine support, and more. To use Express.js to develop a web application, you need to install the module first.

npm install express

Once the module is installed, you can create a simple web application with the following code:

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

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

// Start the server
app.listen(3000, () => {
  console.log('Example app listening on port 3000!');
});

This code will create a web server that listens on port 3000 and responds with "Hello World!" when accessed.

The code contains the following parts:

  1. require('express'): This loads the Express.js module.
  2. const app = express(): This creates an Express.js application.
  3. app.get('/', (req, res) => { ... }): This sets up a route for the root path (/) that will execute a callback function when accessed.
  4. res.send('Hello World!'): This is the response sent to the client when the route is accessed.
  5. app.listen(3000, () => { ... }): This starts the web server and listens on port 3000.

For more information on Express.js, please refer to the official documentation.

Edit this code on GitHub