Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for How to create framework in Node.js
Jahongir Sobirov
Jahongir Sobirov

Posted on

     

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 namedindex.js for it. Then create a file namedtest.js.
Files
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
Enter fullscreen modeExit fullscreen mode

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

functioninServer(self){// This general constructor function};
Enter fullscreen modeExit fullscreen mode

Within the general constructor function, we declare variables namedserverSettings andserver.

functioninServer(self){varserverSetting;varserver;};
Enter fullscreen modeExit fullscreen mode

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

varserverSettings=function(req,res){res.write();res.end();}
Enter fullscreen modeExit fullscreen mode

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();}
Enter fullscreen modeExit 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.

functioninServer(self){varserverSettings=function(req,res){res.write(self['write']);res.end();};varserver=http.createServer(serverSettings);};
Enter fullscreen modeExit 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 thelisten method to theserver variable and take theport object of theself parameter in ourinServerfunction as a parameter:

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

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

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

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}
Enter fullscreen modeExit fullscreen mode

The syntax of our framework (intest.js):

constapp=require("./index.js");vartest=app.inServer({write:"Hello, world",port:8000});
Enter fullscreen modeExit fullscreen mode

Here is the result:

Alt Text
Thank you for your attention!

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
redhoodjt1988 profile image
Jonathan Reeves
I use JavaScript/TypeScript with React and React Native. I prefer MongoDB over any other database. I have recently switched to Go as my language of choice as I am now a DevOps Engineer.

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.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Full stack developer and schools student
  • Location
    Navoi, Uzbekistan
  • Work
    Full Stack programmer at Stylezator Group Inc
  • Joined

More fromJahongir Sobirov

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp