A Beginner’s Guide to Node.js Programming on Windows

Node.js is a powerful and popular runtime for building scalable server-side applications using JavaScript. Whether you’re new to programming or transitioning from front-end development, this guide will walk you through the process of getting started with Node.js, setting up a project, understanding basic routing, and installing packages.

By the end of this tutorial, you’ll be comfortable creating and running simple Node.js applications, setting up routes, and managing packages.


Step 1: Installing Node.js on Windows

Before you can start developing with Node.js, you need to install it on your system.

  1. Download Node.js:
    • Visit the official Node.js website.
    • Download the LTS (Long-Term Support) version, which is more stable for beginners.
  2. Install Node.js:
    • Run the installer and follow the setup instructions.
    • During the installation, Node.js will also install npm (Node Package Manager), which is essential for managing packages.
  3. Verify Installation: After installation, open Command Prompt or PowerShell and type the following commands to verify that Node.js and npm are installed correctly:
    node -v
    npm -v

    You should see the versions of Node.js and npm.


Step 2: Setting Up Your First Node.js Project

Now that you have Node.js installed, let’s set up a simple Node.js project.

  1. Create a Project Folder: Open the command prompt and create a new directory for your project:
    mkdir my-node-app
    cd my-node-app
  2. Initialize the Project: Inside the project folder, run the following command to initialize a new Node.js project. This will create a package.json file, which keeps track of your project’s metadata and dependencies.
    npm init -y

    The -y flag automatically accepts default settings for package.json. You can manually modify the file later if needed.

  3. Step 3: Writing Your First Node.js Application

    Let’s write a simple Node.js application that creates a basic HTTP server.

    1. Create the Main File: Create a file called app.js in your project folder:
      touch app.js
    2. Write Your First Node.js Program:Open app.js in a text editor (e.g., VS Code) and add the following code to create a simple HTTP server:
      const http = require('http');
      const hostname = '127.0.0.1';
      const port = 3000;
      const server = http.createServer((req, res) => {
         res.statusCode = 200;
          res.setHeader('Content-Type', 'text/plain');
         res.end('Hello, World!\n');
      });
      server.listen(port, hostname, () => {
      console.log(`Server running at http://${hostname}:${port}/`);
      });
    3. Run the Application: Save the file and run the program using Node.js:
      node app.js

      If everything is set up correctly, your terminal will display:

      Server running at http://127.0.0.1:3000/
    4. Access the Server: Open your browser and go to http://127.0.0.1:3000. You should see a message saying Hello, World!.

      Step 4: Installing and Using Packages

      Node.js has a vast ecosystem of packages that you can use to simplify development. You can install packages using npm.

      1. Install Express.js: Let’s use Express.js, a popular framework, to handle routing more easily.Run the following command to install it:
        npm install express

        This will create a node_modules folder containing the package, and package.json will now list express as a dependency.

      2. Using Express.js: Let’s update our application to use Express.js instead of the built-in http module for handling requests.Update the content of app.js to:
        const express = require('express');
        const app = express();
        const port = 3000;
        app.get('/', (req, res) => {
        res.send('Hello, Express!');
        });
        app.listen(port, () => {
        console.log(`Express server running at http://localhost:${port}/`);
        });
      3. Run the Application: Run your Node.js server again:
        node app.js

        Go to http://localhost:3000 in your browser, and now you’ll see the message

        Hello, Express!.


      Step 5: Understanding Routing in Express

      Routing refers to defining endpoints for your server where users can send requests to interact with your application. Express makes routing easy.

      1. Define Routes: You can create multiple routes for different paths and HTTP methods. Modify app.js like this:
        const express = require('express');
        const app = express();
        const port = 3000;
        // Define routes
        app.get('/', (req, res) => {
        res.send('Welcome to the Home Page!');
        });
        app.get('/about', (req, res) => {
        res.send('This is the About Page.');
        });
        app.get('/contact', (req, res) => {
        res.send('Contact us at contact@domain.com');
        });
        app.listen(port, () => {
        console.log(`Server is running at http://localhost:${port}/`);
        });
      2. Access Different Routes:
        • Open http://localhost:3000/ to see the home page.
        • Open http://localhost:3000/about to see the about page.
        • Open http://localhost:3000/contact to see the contact page

      How to start a node server as a windows service.

      npm install node-windows

      Create a code (nodeservice.js) as follows 

      const Service = require(‘node-windows’).Service
      const svc = new Service({
      name: “Group task nodeJS”,
      description: “Group task created using Node JS running on 3000”,
      script: “C:\\my-node-app\\task-app\\server.js”
      })

      svc.on(‘install’, function() {
      svc.start()
      })

      svc.install()

      c:\> node nodeservice.js

      this will create a exe file under daemon folder and ask for few windows permissions.

      Go to services.msc and find the service named nodeservice and start it.