This content originally appeared on DEV Community and was authored by Jahongir Sobirov
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 named index.js
for it. Then create a file named test.js
.
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
We create a general constructor function. The name of our common constructor function will be inServer
.
function inServer(self){
// This general constructor function
};
Within the general constructor function, we declare variables named serverSettings
and server
.
function inServer(self){
var serverSetting;
var server;
};
In the serverSettings
variable, we enter what happens on the http server.
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.
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.
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:
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:
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
):
const app = require("./index.js");
var test = app.inServer({
write: "Hello, world",
port: 8000
});
Here is the result:
This content originally appeared on DEV Community and was authored by Jahongir Sobirov
Jahongir Sobirov | Sciencx (2021-07-03T11:08:11+00:00) How to create framework in Node.js. Retrieved from https://www.scien.cx/2021/07/03/how-to-create-framework-in-node-js/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.