Online visitors

People learning around the world

Online

—

Visitors exploring W3Schools

Online visitors by country Circles show approximate country locations. The country list provides the visitor counts.

Circles represent countries, not exact visitor locations.

Country

Online

Loading country activity…

    Express.js Set Up

    Express.js is a minimalist web framework for Node.js, designed to create web applications and RESTful APIs. Its simplicity and flexibility have made it a popular choice among developers. This tutorial will guide you through setting up an Express.js environment.

    Setting Up Express.js

    Prerequisites

    Before setting up Express.js, ensure you have the following installed:

    1. Node.js: Download and install from nodejs.org.
    2. npm: Node Package Manager, included with Node.js.

    Setting up Express.js, the renowned Node.js web application framework, is straightforward. By following a few simple steps, you can quickly establish the framework and start building your web applications easily.

    Step 1: Create a New Project Directory

    Create a new directory for your project and navigate into it using the terminal:

    mkdir my-express-app
    cd my-express-app
    

    Step 2: Initialize a New Node.js Project

    Initialize a new Node.js project by creating a package.json file. Run the following command and follow the prompts:

    npm init
    

    The command above will prompt you for information about your project. You can accept the defaults or provide specific details.

    Step 3: Install Express.js

    Install Express.js using npm:

    npm install express --save
    

    This command above adds Express.js as a dependency in your package.json file.

    Step 4: Create the Server File

    Set up your Express.js server by creating a new file named app.js in your project directory.

    // Import Express.js module
    const express = require('express');
    
    // Initialize the Express application
    const app = express();
    
    // Define the port number for the server to listen on
    const port = 3000;
    
    // Define a route for the root URL ('/') and specify the response
    app.get('/', (req, res) => {
      res.send('Hello, World!'); // Send 'Hello, World!' as the response
    });
    
    // Start the server and have it listen on the defined port
    app.listen(port, () => {
      console.log(`Server is running on http://localhost:${port}`); // Log a message to the console indicating the server is running
    });

    Step 5: Run the Server

    Run your Express.js server using the following command:

    node app.js
    

    Open your web browser and visit http://localhost:3000. The page should show the text "Hello, World!"

    Step 6: Reflecting Changes

    If you make changes to app.js, simply refreshing the page in your browser will not reflect these changes. You must restart the server for changes to take effect. To restart the server:

    1. Stop the running server by pressing Ctrl+C in the terminal.
    2. Start the server again using:
    node app.js
    

    Step 7: Using Nodemon for Automatic Restart

    When changes are made to app.js in a Node.js application, you can automatically restart the server using Nodemon. The Nodemon is a development tool that automatically restarts the Node.js application when it detects changes to any project file, so you don't need to restart the server manually. Install Nodemon globally:

    npm install -g nodemon
    

    Alternatively, install it as a development dependency:

    npm install --save-dev nodemon
    

    Use Nodemon to start your server:

    nodemon app.js
    

    Now, any changes you make to app.js will automatically restart the server and reflect in your browser.

    Conclusion

    In this tutorial, you have learned how to set up an Express.js environment, including installing the necessary components, creating a server, and understanding the basic code structure. With this foundation, you can start developing more complex applications using Express.js.



    Found This Page Useful? Share It!
    Get the Latest Tutorials and Updates
    Join us on Telegram

    Notifications

    Updates are enabled for this browser.

    Receive new tutorials, articles and learning resources. You can turn updates off anytime.