ScanSkill
Sign up for daily dose of tech articles at your inbox.
Loading

How to Create a Nodejs Server Without The Express Framework

How to Create a Nodejs Server Without The Express Framework
How to Create a Nodejs Server Without The Express Framework

Let’s learn how to create a Nodejs server without the express framework. We will be using the HTTP module provided by the node language.

Introduction

Nodejs provides an HTTP module out of the box. Also, the same HTTP module is extended by frameworks like Express, Fastify to create server environments for nodejs. The HTTP module has the functionality that makes it possible to create a web server, handle user requests, and respond back to the user.

In this article, we will look at creating a web server without the use of the express framework.

Prerequisites

Creating a Nodejs server without the express framework

Firstly you need to have Nodejs installed on your machine. If not you can download and install nodejs from here. After the installation is complete, then create a node project.

## creating a dirrectory for node project
$ mkdir nodejs-without-express
$ cd nodejs-without-express

## creating a node project
$ npm init --y

## creating a starting point for out node application
$ touch index.js

## open the project with your favourite code editor 
$ code .

Now that the nodejs application is created with a start point index.js, edit your package.json file to create a start script.

{
  "name": "nodejs-without-express",
  "version": "1.0.0",
  "description": "nodejs server without the express framework",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

The application can now be started by the code npm start. As the node project is set up along with the script let’s create our first nodejs server.

//* index.js

const http = require("http");
const PORT = process.env.PORT || 3000;

//* creates a server
const server = http.createServer((req, res) => {
  //* your route handler codes go here
});

//* tells the server to listen on specified port
server.listen(PORT, () => console.log(`SERVER STARTED ON PORT : ${PORT}`));

Now that the simple application is set up let’s start the code.

## starting the node project
$ npm start
Create a Nodejs Server Without The Express Framework
Starting your first HTTP server

Now let’s serve HTML content in the URL route /html, and for that, let’s add the code in index.js where created out server.

//* index.js

const server = http.createServer((req, res) => {
  const url = req.url;
  if (url === "/html") {
    res.setHeader("Content-Type", "text/html");
    res.write(`
      <html>
        <head>
          <title>Nodejs Server Without The Express Framework</title>
        </head>
        <body>
          <h1>Serving Html</h1>
        </body>
      </html>
    `);
    res.end();
  }
});

Result of serving HTML

Serving HTML content through the use of the HTTP module
Serving HTML content through the use of the HTTP module

JSON documents are also served in the same way. Let’s look at reading the package.json file and serving it to the client as a JSON payload. For achieving that we need to add another module which is fs, it is a module provided by nodejs language to handle files of the file system.

//* index.js

const fs = require("fs");

const server = http.createServer((req, res) => {
  const url = req.url;
  if (url === "/json") {
    res.setHeader("Content-Type", "application/json");
    res.write(fs.readFileSync("./package.json"));
    res.end();
  }
});

Result of serving JSON

Serving the file packge.json to the client as a JSON document
Serving the file packge.json to the client as a JSON document

We successfully served the HTML page at the route /html, and served the JSON document at the route /json. Also, for different HTTP methods, we have access to the methods through the req.method it will provide us with information about the method used by the client.

Conclusion

So in this article, we learned how to create a Nodejs server without the express framework. From this, we saw that we don’t really need to use frameworks for creating a web application in nodejs. As a matter of fact, the frameworks are meant for convenience and may create unnecessary overhead while using them in smaller projects.

Sign up for daily dose of tech articles at your inbox.
Loading