Node.js Server Setup: Running Your First Web Server

Vishesh Singh
3 min readMay 15, 2023

--

In this blog, we will delve into launching a server using Node.js, a widely used JavaScript runtime environment. We’ll start by covering the installation of Node.js and demonstrate how to create a basic server that responds with a “Hello, world!” message for every URL.

Furthermore, we’ll explore how to handle custom responses based on different URLs. While the process can be intricate, we’ll introduce a time-saving bonus that simplifies the setup.

Let’s get started!

1. Prerequisites:

Installing Node.js Before we begin, ensure that Node.js is installed on your machine. Follow these steps:

  1. Visit the official Node.js website: https://nodejs.org/
  2. Download the appropriate installer for your operating system.
  3. Run the installer and follow the installation wizard instructions.
  4. Verify the installation by opening a terminal or command prompt and typing the following command:
node -v

If you see the Node.js version number, the installation was successful.

1.1 Node.js Server Setup:

To start a server using Node.js and the http module, follow these steps:

  1. Set Up a New Node.js Project: Create a new directory for your project. Open a terminal or command prompt, navigate to the project directory, and run the following command to initialize a new Node.js project:
npm init -y

2. Create a Server File: Create a new file, e.g., server.js, in your project directory. This file will contain the code to start the server.

3. Write Server Code: Open server.js and import the http module. Create an instance of the server and define a request handler function that responds with "Hello, world!" for any URL. Here's an example of a simple server code:

const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello, world!');
});
const port = 3000;
server.listen(port, () => {
console.log(`Server is running on port ${port}`);
});

4. Start the Server: In your terminal or command prompt, run the following command to start the server:

node server.js

5. Test the Server: Open a web browser and visit http://localhost:3000. You should see the message "Hello, world!" displayed in the browser.

2. Customize Response Based on URL:

To customize the server’s response based on the URL, you can access the req.url property within the request handler function. Modify the server code as follows:

const http = require('http');
const server = http.createServer((req, res) => {
if (req.url === '/home') {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Welcome to the home page!');
} else if (req.url === '/contact') {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Contact us at contact@example.com');
} else {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Page not found');
}
});
const port = 3000;
server.listen(port, () => {
console.log(`Server is running on port ${port}`);
});

Further Reading: Official Node.js Documentation

Bonus: Using Express to Simplify the Process

Starting a server using plain Node.js can be cumbersome, especially for larger applications. To simplify the process, we can use Express, a fast and minimalist web framework for Node.js. Express provides a higher-level abstraction over the built-in http module, making server setup and routing more intuitive.

For a detailed guide on starting a server using Express, refer to the blog: “Simplifying Server Creation: Mastering Express Basics”

Starting a server using Node.js is an essential skill for web developers. While the process can be complex, utilizing frameworks like Express can greatly simplify server setup and routing. By following the steps outlined in this blog, you can begin your journey into Node.js server development. Happy coding!

Thank you for reading! If you have any questions or need further clarification, please feel free to leave a comment below.

--

--

Vishesh Singh
Vishesh Singh

Written by Vishesh Singh

Full Stack Dev || Tech Enthusiast

No responses yet