JavaScript Q&A Logo
JavaScript Q&A Part of the Q&A Network

What is Express.js and how do I create a basic app?

Asked on Sep 19, 2024

Answer

Express.js is a minimal and flexible Node.js web application framework that provides a robust set of features for building web and mobile applications. Here's how you can create a basic Express.js app:
<!-- BEGIN COPY / PASTE -->
        const express = require('express');
        const app = express();
        const port = 3000;

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

        app.listen(port, () => {
          console.log(`Example app listening at http://localhost:${port}`);
        });
        <!-- END COPY / PASTE -->
Additional Comment:
  1. Install Express.js using npm with the command: "npm install express".
  2. The code creates a simple server that listens on port 3000.
  3. When accessing the root URL ("/"), it responds with "Hello World!".
  4. Run the app using the command: "node filename.js", replacing "filename.js" with your file's name.
✅ Answered with JavaScript best practices.
← Back to All Questions