Skip to main content

Node server without using Express

Introduction

In previous docs, we learned to create backend applications using Express.js. In this doc, we will learn how to create a Node.js server without using Express.js.

note

Perhaps you won't be using this approach in your projects, but it is good to know how things work under the hood.

Let's get started

Create a new folder

mkdir node-server-without-express

Navigate to the folder,

cd node-server-without-express

Run the following command to initialize the project.

npm init -y

Modify the package.json file, add the type as module, and add the start, and dev scripts.

package.json
{
"name": "node-server-without-express",
"version": "1.0.0",
"description": "",
"type": "module",
"main": "index.js",
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js"
},
"keywords": [],
"author": "",
"license": "ISC"
}

Create a new file index.js and add the following code.

index.js
import http from "http";

const server = http.createServer((req, res) => {
res.end("Hello World");
});

server.listen(3000);
note

You don't need to install the http module as it is a core module of Node.js. It's already there in your system when you install Node.js.

Now, we can run the application by running the following command:

npm run dev

Once the server is up and running, you can go to your browser and access the URL: http://localhost:3000

Adding multiple APIs

Let's add multiple APIs to our server. For this, I will modify the index.js file as follows:

index.js
import http from "http";

const server = http.createServer((req, res) => {
// Check the url
if (req.url === "/") {
// Setting the header
res.writeHead(200, { "Content-Type": "application/json" });
// Sending the response
res.end(JSON.stringify({ message: "Hello, world!" }));
} else if (req.url === "/about") {
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({ message: "About page" }));
} else {
res.writeHead(404, { "Content-Type": "application/json" });
res.end(JSON.stringify({ message: "Page not found" }));
}
});

server.listen(3000);

Here, I am checking the URL and sending the response accordingly. For now, I created three APIs:

  • / - This will return the message Hello, world!
  • /about - This will return the message About page
  • Any other URL - This will return the message Page not found

If you run the application and access the URL: http://localhost:3000, you will get the following response:

Response
{
"message": "Hello, world!"
}

If you access the URL: http://localhost:3000/about, you will get the following response:

Response
{
"message": "About page"
}

If you access any other URL, you will get the following response:

Response
{
"message": "Page not found"
}

Limitations

As you can see, we are checking the URL and sending the response accordingly. This approach will work fine for a small application, but as the application grows, it will become difficult to manage the code. Also, we are not handling the request methods. For example, if we want to create an API that will return the user details, then we will have to create a new route for that. We will have to check the URL and the request method, and then send the response accordingly. This will make the code more complex and difficult to manage.

Code

You can download the related code from here

Summary

In this doc, we learned how to create a Node.js server without using Express.js. We also learned how to add multiple APIs to our server. In the next blog, we will learn how to create a Node.js server using Express.js.