How to create framework in Node.js
Tags: node, javascript, http
Published: 2021-07-03 11:08:11

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](https://nodejs.org/en/download/)
![Node.js download](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oxi575ikru41faf19fhn.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8815ufuyth5bowl85015.png)
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:
```js
const http = require('http'); // Add the http module
```
We create a general constructor function. The name of our common constructor function will be `inServer`.
```js
function inServer(self){
   // This general constructor function
};
```
Within the general constructor function, we declare variables named `serverSettings` and `server`.
```js
function inServer(self){
   var serverSetting;
   var server;
};
```
In the `serverSettings` variable, we enter what happens on the http server.
```js
var serverSettings = function(req, res){
   res.write();
   res.end();
}
```
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.
```js
var serverSettings = function(req, res){
   res.write(self['write']);
   res.end();
}
```
We write the value in the serverSettings variable as a parameter to the createServer method of the http module in the server variable.
```js
function inServer(self){
   var serverSettings = function(req, res){
      res.write(self['write']);
      res.end();
   };
   var server = http.createServer(serverSettings);
};
```
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 `inServer`function as a parameter:
```js
server.listen(self["port"]);
```
To use our framework as a module, we write the inSever function as a module function:
```js
module.exports = {
   inServer
}
```
Overview of our framework code:
```js
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
}
```
The syntax of our framework (in `test.js`):
```js
const app = require("./index.js");
var test = app.inServer({
   write: "Hello, world",
   port: 8000
});
```
#### Here is the result:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dvpc7u4zq6jnbrpawua2.png)
Thank you for your attention!