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.
- Download Node.js:
- Visit the official Node.js website.
- Download the LTS (Long-Term Support) version, which is more stable for beginners.
- 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.
- 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.
- Create a Project Folder: Open the command prompt and create a new directory for your project:
mkdir my-node-app
cd my-node-app
- Initialize the Project: Inside the project folder, run the following command to initialize a new Node.js project. This will create a
package.jsonfile, which keeps track of your project’s metadata and dependencies.npm init -y
The
-yflag automatically accepts default settings forpackage.json. You can manually modify the file later if needed. -
Step 3: Writing Your First Node.js Application
Let’s write a simple Node.js application that creates a basic HTTP server.
- Create the Main File: Create a file called
app.jsin your project folder:touch app.js
- Write Your First Node.js Program:Open
app.jsin 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}/`);});
- 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/
- 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.
- 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_modulesfolder containing the package, andpackage.jsonwill now listexpressas a dependency. - Using Express.js: Let’s update our application to use Express.js instead of the built-in
httpmodule for handling requests.Update the content ofapp.jsto: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}/`);
});
- Run the Application: Run your Node.js server again:
node app.js
Go to
http://localhost:3000in your browser, and now you’ll see the messageHello, 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.
- Define Routes: You can create multiple routes for different paths and HTTP methods. Modify
app.jslike 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}/`);
});
- Access Different Routes:
- Open
http://localhost:3000/to see the home page. - Open
http://localhost:3000/aboutto see the about page. - Open
http://localhost:3000/contactto see the contact page
- Open
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.jsthis 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.
- Install Express.js: Let’s use Express.js, a popular framework, to handle routing more easily.Run the following command to install it:
- Create the Main File: Create a file called




