How to create framework in Node.js

jahongir2007

Jahongir Sobirov

Posted on July 3, 2021

How to create framework in Node.js

Are you interested in creating frameworks? Then read this post carefully.

What is framework?

Let's find out what the framework itself is. A framework is a program that combines one or more functions, simplifying the work of a programmer. The framework works in the same programming language in which it was written. (The framework we create for Node.js is written in JavaScript)

What is Node.js?

Node.js is a runtime program that allows you to use the javascript programming language outside the browser. It contains several modules, such as http and so on. Popular apps created at Node.js: Linkedin, Netflix, Ebay and Uber. The most popular frameworks of this program are Express.js, Telegraf.js and so on. All frameworks written in JS also work in Node.js.

Download Node.js

Node.js download
When you download Node.js, you will be given the choice of LTS or Current version. I advise you to choose the LTS i.e. Long Time Support version. Because it will be fully tested.

Create framework

Now all that remains is to think about one thing. What does our framework do? Why are we creating it? Remember to set a goal no matter what program you create! Our framework is designed to create simple http servers. Now we can create a folder of our framework: httpwork (this is the name of our framework). Now create a file named index.js for it. Then create a file named test.js.
Files
We save the framework we are creating in index.js and test it in test.js.

In index.js:

We use the http module to create our framework:

const http = require('http'); // Add the http module
Enter fullscreen mode Exit fullscreen mode

We create a general constructor function. The name of our common constructor function will be inServer.

function inServer(self){
   // This general constructor function
};
Enter fullscreen mode Exit fullscreen mode

Within the general constructor function, we declare variables named serverSettings and server.

function inServer(self){
   var serverSetting;
   var server;
};
Enter fullscreen mode Exit fullscreen mode

In the serverSettings variable, we enter what happens on the http server.

var serverSettings = function(req, res){
   res.write();
   res.end();
}
Enter fullscreen mode Exit fullscreen mode

In the write() method, we specify that the self parameter in the inServer function must retrieve information from the write object. Our framework can retrieve user input using the self parameter.

var serverSettings = function(req, res){
   res.write(self['write']);
   res.end();
}
Enter fullscreen mode Exit fullscreen mode

We write the value in the serverSettings variable as a parameter to the createServer method of the http module in the server variable.

function inServer(self){
   var serverSettings = function(req, res){
      res.write(self['write']);
      res.end();
   };
   var server = http.createServer(serverSettings);
};
Enter fullscreen mode Exit fullscreen mode

Enter on which port of the http server our framework works (This is also entered by the user). To do this, we write the listen method to the server variable and take the port object of the self parameter in our inServerfunction as a parameter:

server.listen(self["port"]);
Enter fullscreen mode Exit fullscreen mode

To use our framework as a module, we write the inSever function as a module function:

module.exports = {
   inServer
}
Enter fullscreen mode Exit fullscreen mode

Overview of our framework code:

const http = require('http');

function inServer(self){
   var serverSettings = function(req, res){
      res.write(self['write']);
      res.end();
   };
   var server = http.createServer(serverSettings);
   server.listen(self["port"]);
};

module.exports = {
   inServer
}
Enter fullscreen mode Exit fullscreen mode

The syntax of our framework (in test.js):

const app = require("./index.js");
var test = app.inServer({
   write: "Hello, world",
   port: 8000
});
Enter fullscreen mode Exit fullscreen mode

Here is the result:

Alt Text
Thank you for your attention!

💖 💪 🙅 🚩
jahongir2007
Jahongir Sobirov

Posted on July 3, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related