
Node.js is a powerful runtime environment that allows you to run JavaScript code on the server-side. It has gained immense popularity due to its efficiency and scalability. In this tutorial, we will walk through the process of building your first Node.js application. By the end, you will have a solid understanding of the fundamentals and be ready to explore more advanced concepts.
Prerequisites:
Before diving into Node.js, make sure you have the following installed on your computer:
- Node.js (latest stable version)
- Text editor or integrated development environment (IDE) of your choice
Step 1: Setting Up Your Node Project
1. Open your terminal or command prompt.
2. Create a new directory for your project: mkdir node
3. Navigate into the newly created directory: cd node
4. Initialize a new Node.js project by running:
npm init
5. Follow the prompts and provide necessary information for your project.
6. Once the initialization is complete, you will have a package.json file in your project directory.

Step 2: Creating a server using Node
1. Create a new file called server.js in your project directory.
2. Open server.js in your text editor/IDE.
3. Add the following code to create a basic HTTP server:
// Import the 'http' module
const http = require('http');
// Create an HTTP server
const server = http.createServer((req, res) => {
// Set the status code to 200 (OK)
res.statusCode = 200;
// Set the response header content type to plain text
res.setHeader('Content-Type', 'text/plain');
// Write the response body
res.end('Hello, World!');
});
// Specify the port for the server to listen on
const port = 3000;
// Start the server and listen for incoming requests
server.listen(port, () => {
// Display a message in the console once the server starts successfully
console.log(`Server running at http://localhost:${port}/`);
});
Step 3: Starting a server using Node
1. Save the changes you made to server.js.
2. In your terminal or command prompt, navigate to your project directory (node).
3. Start the server by running:
node server.js
4. You should see a message indicating that the Server is running at http://localhost:3000/.

Step 4: Testing a server using Node
1. Open your web browser and visit http://localhost:3000/.
2. You should see the message “Hello, World!” displayed on the page.

Congratulations!
You have successfully created and tested your first Node.js server.
