Skip to main content

Creating a Simple Server using Nodejs

Creating a Simple Server using Node.js

Node.js is a powerful JavaScript runtime built on Chrome's V8 JavaScript engine. It allows you to build scalable network applications using JavaScript on the server-side. In this tutorial, we'll walk through the process of creating a simple server using Node.js.

Prerequisites

Before we get started, make sure you have Node.js and npm (Node package manager) installed on your computer. If you haven't installed them yet, you can download them from https://nodejs.org/.

Step 1: Initialize a New Node.js Project

First, we'll create a new directory for our project and initialize a new Node.js project. Open your terminal and run the following commands:

mkdir node-server
cd node-server
npm init -y

The npm init -y command creates a new package.json file in your project directory. This file contains information about your project and its dependencies.

Step 2: Create a New Server File

Next, we'll create a new file in our project directory where we'll write the code for our server. You can name this file anything you like, but for this tutorial, we'll call it server.js.

touch server.js

Step 3: Write the Server Code

Open the server.js file in your favorite text editor and add the following code:

const http = require('http');

const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});

const PORT = process.env.PORT || 3000;

server.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});

This code does the following:

  • It imports the http module, which provides the functionality to create a HTTP server.
  • It uses the http.createServer() method to create a new server instance. This method takes a callback function that is called once for every request to your server. The callback function takes two arguments: a request object (which contains information about the incoming request) and a response object (used to send data back to the client).
  • It sets the response status code to 200 (which means "OK") and the response content type to text/plain.
  • It sends a response containing the string 'Hello World\n'.
  • It sets the port number that our server will listen on. It first checks if there's a PORT environment variable set (useful when deploying to a hosting provider), if not, it defaults to port 3000.
  • It starts the server and listens on the specified port. When the server is ready, it logs a message to the console.

Step 4: Run the Server

To start the server, go back to your terminal and run the following command:

node server.js

You should see the message Server running on port 3000 in your terminal. Now, if you open a web browser and navigate to http://localhost:3000, you should see a page displaying the text "Hello World".

Congratulations! You just created a simple HTTP server using Node.js. You can now use this as a starting point to build more complex applications.

Remember, Node.js is a powerful tool for building network applications. It's used by many large companies and is a great addition to any developer's toolkit. Happy coding!