
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
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 namedindex.js
for it. Then create a file namedtest.js
.
We save the framework we are creating inindex.js
and test it intest.js
.
Inindex.js
:
We use the http module to create our framework:
consthttp=require('http');// Add the http module
We create a general constructor function. The name of our common constructor function will beinServer
.
functioninServer(self){// This general constructor function};
Within the general constructor function, we declare variables namedserverSettings
andserver
.
functioninServer(self){varserverSetting;varserver;};
In theserverSettings
variable, we enter what happens on the http server.
varserverSettings=function(req,res){res.write();res.end();}
In thewrite()
method, we specify that theself
parameter in theinServer
function must retrieve information from the write object. Our framework can retrieve user input using the self parameter.
varserverSettings=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.
functioninServer(self){varserverSettings=function(req,res){res.write(self['write']);res.end();};varserver=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 thelisten
method to theserver
variable and take theport
object of theself
parameter in ourinServer
function as a parameter:
server.listen(self["port"]);
To use our framework as a module, we write the inSever function as a module function:
module.exports={inServer}
Overview of our framework code:
consthttp=require('http');functioninServer(self){varserverSettings=function(req,res){res.write(self['write']);res.end();};varserver=http.createServer(serverSettings);server.listen(self["port"]);};module.exports={inServer}
The syntax of our framework (intest.js
):
constapp=require("./index.js");vartest=app.inServer({write:"Hello, world",port:8000});
Here is the result:
Top comments(1)

- Email
- LocationCA
- EducationLambda School
- WorkDevOps Engineer
- Joined
This was a good article. Very easy to follow and I feel like it made the simple point of walking through a very basic framework setup. Thank you for writing this.
For further actions, you may consider blocking this person and/orreporting abuse