Node.js is a powerful JavaScript runtime that allows you to run JavaScript on the server side. Setting up a Node.js server is an essential skill for building dynamic web applications. This guide will walk you through the steps to install Node.js, set up a simple server, and handle basic routes.

Prerequisites

  • A computer with internet access
  • Basic knowledge of JavaScript
  • A code editor (e.g., Visual Studio Code, Sublime Text)

Step 1: Install Node.js

For Windows and macOS

  1. Download the Installer:
    Go to the Node.js official website and download the latest version of the Node.js installer for your operating system.

  2. Run the Installer:
    Follow the prompts in the installer to complete the installation. Make sure to check the box that says "Automatically install the necessary tools" if prompted.

  3. Verify the Installation:
    Open your terminal or command prompt and run the following commands to verify that Node.js and npm (Node Package Manager) are installed:

    node -v
    npm -v
    

For Linux

  1. Update Package Index:
    Open your terminal and run:

    sudo apt update
    
  2. Install Node.js:
    You can install Node.js from the default repositories using:

    sudo apt install nodejs npm
    
  3. Verify the Installation:
    Check if Node.js and npm were installed correctly:

    node -v
    npm -v
    

Step 2: Create a New Project

  1. Create a New Folder for Your Project:
    Create a new directory for your Node.js server. For example:

    mkdir my-node-server
    cd my-node-server
    
  2. Initialize the Project:
    Initialize a new Node.js project by running:

    npm init -y
    

    This command will create a package.json file with default settings.

Step 3: Install Express.js

Express.js is a popular web framework for Node.js that simplifies server creation and routing.

  1. Install Express:
    Run the following command to install Express:

    npm install express
    

Step 4: Set Up a Simple Server

  1. Create a Server File:
    Create a new file named server.js in your project folder.

  2. Add Server Code:
    Open server.js and add the following code:

    const express = require('express');
    const app = express();
    const PORT = process.env.PORT || 3000;
    
    // Middleware to parse JSON
    app.use(express.json());
    
    // Basic route
    app.get('/', (req, res) => {
        res.send('Welcome to My Node.js Server!');
    });
    
    // Start the server
    app.listen(PORT, () => {
        console.log(`Server is running on http://localhost:${PORT}`);
    });
    

Step 5: Run Your Server

  1. Start the Server:
    In your terminal, run the following command to start the server:

    node server.js
    
  2. Access Your Server:
    Open your web browser and go to http://localhost:3000. You should see the message "Welcome to My Node.js Server!"

Step 6: Handle Basic Routes

You can add more routes to your server to handle different requests. Here are examples of how to handle different routes:

  1. Add More Routes:
    Update your server.js file to include additional routes:

    // About route
    app.get('/about', (req, res) => {
        res.send('This is the About page.');
    });
    
    // Contact route
    app.get('/contact', (req, res) => {
        res.send('This is the Contact page.');
    });
    
  2. Test the Routes:

    • Go to http://localhost:3000/about to see the About page.
    • Go to http://localhost:3000/contact to see the Contact page.

Conclusion

You have successfully set up a Node.js server using Express.js! By following this guide, you learned how to install Node.js, create a simple server, and handle basic routes for dynamic web applications. You can now expand your server with additional features and endpoints as needed.