Building a node. js application in a modular manner

Source: Internet
Author: User

Today, almost all Web services or integrations are done at the node. JS Runtime. node. JS is a flexible platform with a lot of community support. We can even create Xlsx,docx or PDF documents directly from node. js. All major cloud platforms can use node. js as its Level 1 language.

Modular

Node. JS is designed to be modular using the node_modules structure. All the necessary modules are stored in the Node_modules directory, and we can call them anywhere in the code.

Now, we'll use this modular approach in our application code. Most of the applications I see contain a Lib folder, where all the JS files are stored. These JS files use relative paths to import the desired region.

Require (".. /db/")require (".. /.. /logging ")  

The main problem with this approach is that when we change the path of the service file, the path to the DB should also change. Additionally, the format is unreadable. We will be confused about the authenticity of the document.

Solution Solutions

A better approach is to design the application as a module, such as DB, logging, errors, and so on. Assuming that your application name is CMS, using scope makes it easier to represent the module.

Require ("@cms/db")

You can develop the modules separately and publish them to any NPM server (public/private) and use them like any other module.

If your application requires a logging module:

NPM Install--save @cms/logging

If you don't want to divide the application into parts, there is another way.

More Optimal way

Save the required modules in a separate folder. Suppose there is a "@cms". Use a separate folder for each module, so that the module has a separate package.json. This makes it possible to become a valid node module.

The Package.json of the module will be as follows

{  "@cms/db",  "1.0.1", "  dependencies": {"latest"}}   

Once the module is ready, you can make some scripts. Add the install.js in the Scripts folder.

let fs = require ( ' FS ') console.log (  ' Creating symlinks ... ') if (Fs.existssync ( ' node_modules/@cms ')) {console.log ( ' link exists already ')} else { Span class= "Hljs-keyword" >let Source = . /@cms ' console.log ( ' creating link for ${source} ') Fs.symlinksync (source,  ' node_modules/@cms ',  ' junction ') console.log ( ' Done ')}  

Add this script to main Package.json.

{  "Cmsapplication",  "1.0.1",  "dependencies": {"latest"}}   

This script is executed every time you perform a NPM installation. Therefore, once all other node modules are defined and the dependencies are installed, it creates a link from outside the @cms folder to the internal node_modules of the @cms folder. So any changes you make to the external @cms folder will be reflected in the node_modules inside the folder.

You can see that we have a symbolic link installed on @cms. This is not a shortcut file, not a hard link created with "ln" in Linux.

Inside @cms, you can see the modules we defined in the external @cms folder.

This allows you to achieve modularity. The "@cms" folder is part of your source code. You can then import the required modules as you normally would.

Require ("@cms/logging") logger.info ("Welcome to CMS Application") 

When you want the application to execute, run "NPM Install" and run "npm start".

This approach helps to make your application more modular and extensible. You are welcome to tell your opinion in the comments.

http://wfhuamei.com/
http://yong-elec.com/
http://tlwhcy.com/
http://ipumei.com/
http://qzcgyy.net/
http://cqtgzc.com/
http://myjjcn.com/
http://booioo.net/
http://gdlsly.com/
http://chinasstk.com/
http://metdmr.com/
http://syqixun.com/
http://shkuju.com/
http://rongju55.com/
http://yn698.com/
http://huabaoshicai.cn/
http://hshyin.com/
http://midea.ha.cn/
http://71cf.cn/
http://kmlbzs.com/

Building a node. js application in a modular manner

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.