Building Node.js applications in a modular way

Today, almost all web services or integrations are done on 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 mainstream cloud platforms can use Node.js as their level 1 language.

Modular

By designing Node.js, you can use node_modules structure to achieve modularity. All necessary modules are stored in the node_modules directory, and we can call these modules anywhere in the code.

And now, we will use this modular approach in the application code. Most of the applications I have seen contain a lib folder in which all JS files are stored. These js files import relative areas using relative paths.

const db = require("../db/")
const logging = require ("../../logging")

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

solution

A better approach is to design the application as a module, such as DB, logging, errors, etc. Assuming your application name is cms, then using scope makes it easier to represent modules.

require("@cms/db")

You can develop 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 several parts, there is another way.

The better way

Save the required modules in a separate folder. Suppose there is "@cms". Use a separate folder for each module, so that the module has a separate package.json. This can become an effective Node module.

The module's package.json will look like this

{
  "name": "@cms/db",
  "version": "1.0.1",
  "description": "db module for CMS Application", "main": "index.js", "dependencies":{ "mysql" : "latest" } }

After the module is ready, you can make some scripts. Add 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 { 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.

{
  "name": "CMSApplication",
  "version": "1.0.1",
  "description": "Sample CMS Application", "main": "index.js", "scripts": { "install": "node scripts/install.js", "start": "node index.js" }, "dependencies":{ "express" : "latest" } }

This script will be executed whenever you perform npm installation. Therefore, once all other node modules are defined and dependencies are installed, it will create a link from outside the @cms folder to the node_modules inside 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 installed a symbolic link to @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 way you achieve modularity. The "@cms" folder is part of your source code. Then you can import the required modules in the normal way.

const {logger} = require("@cms/logging")
logger.info("Welcome to CMS Application")

When you want the application to execute, run "npm install" and then "npm start".

This approach helps make applications more modular and extensible. Welcome to share your views in the comments.

Guess you like

Origin www.cnblogs.com/xiaobin123/p/12730172.html