How to Make Your First Node.js Server: Step-by-Step Tutorial

4 min readZeeshan Ali
How to Make Your First Node.js Server: Step-by-Step Tutorial

What is Node.js? Node.js is an open-source, cross-platform, JavaScript runtime environment that executes JavaScript code outside a web browser. It allows developers to build scalable network applications quickly and efficiently. Think of it as JavaScript on the server-side!

Illustration of Node.js Runtime Architecture
Figure 1: Node.js Architecture.

Prerequisites Before we begin, make sure you have a basic understanding of JavaScript. You don't need to be an expert, but familiarity with variables, functions, and control flow will be helpful.

Download and Install Node.js The first step is to get Node.js installed on your system. Visit the official Node.js website and download the recommended LTS (Long Term Support) version for your operating system.

  1. Visit the official website: Go to nodejs.org.
Node.js Official Download Page with LTS download highlighted
Figure 2: Download the LTS version from nodejs.org.
  1. Download the installer: Choose the installer for your operating system (Windows, macOS, Linux).
Screenshot of Node.js Installer Wizard (MSI)
Figure 3: Follow the Node.js MSI Window Setup.
  1. Run the installer: Follow the prompts to complete the installation. It's usually a 'Next, Next, Finish' process.
  1. Verify installation: Open your terminal or command prompt and type node -v and npm -v. You should see the installed versions, confirming Node.js and npm (Node Package Manager) are ready.
Terminal output showing Node.js and npm versions after successful installation
Figure 4: Verify Node.js and npm installation in your terminal.

Project Setup: Create Your Folder and Open in VS Code

First, create a new folder for your project, for example, my-first-server.

Screenshot of an empty 'my-first-server' folder in file explorer
Figure 5: Create a new folder named `my-first-server`.

Now, open this folder in Visual Studio Code. A quick way to do this is to click on the address bar in your file explorer, type cmd, and press Enter. This will open a command prompt in that directory. Then, type code . and press Enter. This command will open the current folder in Visual Studio Code.

Command prompt showing 'code .' command typed in folder path
Figure 6: Use `code .` in the terminal to open the folder in VS Code.
Screenshot of an empty 'my-first-server' folder opened in Visual Studio Code
Figure 7: Your project folder will open in Visual Studio Code.

Minimal Server Code Inside your my-first-server folder, create a new file named server.js. This will be where our server code lives. Now, let's write the code for our basic HTTP server. Paste the following code into your server.js file:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 const http = require('http'); const hostname = '127.0.0.1'; const port = 8080; 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}/`); });
Screenshot of basic Node.js HTTP server code in app.js
Figure 8: Create `server.js` and add the basic HTTP server code.

Code Explanation:

  • const http = require('http');: This line imports Node.js's built-in http module, which provides functionality for creating HTTP servers.
  • const hostname = '127.0.0.1';: This sets the IP address where our server will listen. '127.0.0.1' is the localhost.
  • const port = 3000;: This sets the port number on which our server will listen for incoming requests.
  • const server = http.createServer((req, res) => { ... });: This creates an HTTP server object. The callback function (req, res) is executed every time a request is made to our server.
    • req (request) object: Contains information about the incoming request (e.g., URL, headers).
    • res (response) object: Used to send data back to the client.
  • res.statusCode = 200;: Sets the HTTP status code to 200, indicating a successful response.
  • res.setHeader('Content-Type', 'text/plain');: Sets the content type of the response. Here, we're sending plain text.
  • res.end('Hello World!\n');: Sends the 'Hello World!' text as the response body and closes the connection.
  • server.listen(port, hostname, () => { ... });: Starts the server, making it listen for connections on the specified port and hostname. The callback function is executed once the server starts successfully.

Run Command To run your server, open the integrated terminal in VS Code (Ctrl+J or Terminal > New Terminal) or your system's terminal/command prompt. Navigate to your my-first-server directory if you're not already there, and execute the following command:

1 node server.js

You should see the message Server running at http://127.0.0.1:8080/ in your terminal. This means your server is now active and listening for requests!

Terminal output showing 'Server running at http://127.0.0.1:8088/' after executing node app.js
Figure 9: Running your Node.js server in the terminal.

Now, open your web browser and go to http://127.0.0.1:8080/. You should see 'Hello World!' displayed in your browser. Congratulations, you've successfully created and run your first Node.js server!

Web browser displaying 'Hello World!' text from the Node.js server
Figure 10: 'Hello World!' displayed in your browser.

What is Express.js? Express.js is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. It simplifies the process of building server-side applications with Node.js, offering tools for routing, middleware, and more.

Express.js Logo
Figure 12: The Express.js logo.

Install Express.js To use Express.js in your project, you need to install it using npm. Open your terminal in your project directory (my-first-server) and run the following command:

1 npm install express
Terminal output showing npm install express command
Figure 13: Running the `npm install express` command.

Sample Express Code Create a new file named express-app.js in your project directory and paste the following minimal Express.js server code:

1 2 3 4 5 6 7 8 9 10 11 const express = require('express'); const app = express(); const port = 8080; app.get('/', (req, res) => { res.send('Hello World!') }); app.listen(port, () => { console.log(`Server running at http://${hostname}:${port}/`) });
Screenshot of minimal Express.js server code in VS Code
Figure 14: Sample Express.js code in `express-app.js`.

Command to Run To run your Express.js application, execute the following command in your terminal:

1 node server.js

You should see the message Express app listening at http://localhost:8080 in your terminal. This means your Express.js server is now active!

Terminal output showing Express.js app listening message
Figure 15: Running the Express.js app in the terminal.

URL to Open in Browser Open your web browser and navigate to the following URL:

http://localhost:8080

Browser showing 'Hello World!'
Figure 16: 'Hello World!' displayed in your browser.

Final Thoughts: This is just the beginning! Node.js is incredibly powerful for building all kinds of network applications, from simple APIs to real-time chat applications. Keep exploring the official documentation and building more complex projects.

About the Author: Zeeshan Ali is a passionate developer with expertise in frontend, backend, AI, and blockchain. Explore my open-source projects and tutorials on GitHub: Zeeshan Ali's GitHub Profile. You can also connect with me on LinkedIn: Zeeshan Ali's LinkedIn Profile.

Zeeshan Ali's GitHub Profile for Coding Resources
Figure 11: Visit Zeeshan Ali’s GitHub for more coding resources.

Was this article helpful?

Node.js tutorialbuild first Node.js serverNode.js for beginnersHTTP server Node.jsJavaScript backend developmentExpress.js tutorialNode.js web frameworkserver-side JavaScriptbackend development guideNode.js installationNode.js server setupNode.js code exampleshow to run Node.js serverNode.js API serverweb development backend
Zeeshan Ali profile picture

About Zeeshan Ali

Software Engineer specializing in Web/Mobile Apps, AI, Data Science, and Blockchain. Follow me on GitHub and LinkedIn.

More from Zeeshan Ali

View all articles →