9951 explained code solutions for 126 technologies


expressjsHow can I use the Express.js framework to create a web application?


Express.js is a web application framework for Node.js that simplifies the development of web applications. It provides a robust set of features for web and mobile applications, including routing, middleware, view system, and more.

To create a web application with Express.js, you need to install the framework and set up your project. Here is an example of a simple Express.js application:

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

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

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

The code above will create a simple web application that will listen on port 3000 and respond with "Hello World!" when a request is made to the root URL.

Code explanation

  1. Requiring the Express.js module const express = require('express');
  2. Creating an Express.js application instance const app = express();
  3. Setting up a route to handle requests to the root URL app.get('/', (req, res) => {...});
  4. Sending a response to the request res.send('Hello World!');
  5. Starting the server app.listen(3000, () => {...});

For more information on how to use Express.js, see the Express.js documentation.

Edit this code on GitHub