03_Node js 模块化 CommonJS

619 阅读2分钟

一、什么是 CommonJS?

CommonJS 就是为 JS 的表现来制定规范,因为 JS 没有模块系统,标准库较少,缺乏包管理工具,所以 CommonJS 应运而生,它希望 JS 可以在任何地方运行,而不只是在浏览器中,从而达到 Java、C#、PHP 这些后端语言具备开发大型应用的能力。

二、CommonJS 的应用

  • 服务器端 JavaScript 应用程序(Node.js)。
  • 命令行工具。
  • 桌面图形界面应用程序。

三、CommonJS 与 Node.js 的关系?

CommonJS 就是模块化的标准,Node.js 就是 CommonJS(模块化)的实现。

四、Node.js 中的模块化?

在 Node 中,模块分为两类:

  • 一是 Node 提供的模块,称为核心模块,例如 http 模块、url 模块、fs 模块。

  • 二是用户编写的模块,称为文件模块,比如接下来要新建一个 js 文件,并在里边添加的工具模块,通过 exports 或者 module.exports 将模块导出,并通过 require 引入这些模块。

五、实例

首先新建一个 js 文件,在里边添加工具模块,并暴露模块。

暴露模块的方式有两种:

  • exports
  • module.exports

module.exports 是真正的接口,exports 是一个辅助工具。

如果 module.exports 为空,那么所有的 exports 收集到的属性和方法,都赋值给了 module.exports,如果 module.exports 具有任何属性和方法,则 exports 会被忽略。

1、exports 使用方法:

let str = "learning CommonJS";
exports.str = str; // {str: "learning CommonJS"}

2、module.exports 使用方法:

暴露出 tools 工具模块,通过 require 导入使用(与 url 模块和 http 模块的引入方式相同)。

在下面代码中我们是用 module.exports 方式来暴露模块。

tools.js

const tools = {
    length: (...numbers) => {
        return numbers.length;
    },
    sum: (...numbers) => {
        let sum = 0;
        for (let number in numbers) {
            sum += numbers[number]
        };
        return sum;
    }
};
module.exports = tools;

接下来再新建一个 js 文件,并引入刚编写的模块。

CommonJS.js

const tool = require('./tools.js')
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((request, response) => {
    if (request.url != '/favicon.ico') {
        console.log(tool.length(1,2,3));
        console.log(tool.sum(1,2,3));
    };
    response.statusCode = 200;
    response.setHeader('Content-Type', 'text/plain;charset=utf-8');
    response.end('CommonJS,调取了 tool.js 工具模块中的 length() 和 sum() 两个方法。');
});
server.listen(port, hostname, () => {
    console.log(`服务器运行在 http://${hostname}:${port}`);
});

执行 node CommonJS.js,访问:127.0.0.1:3000/

3
6

期待您的关注!