Node.js 自定义模块
Node.js内置多个模块,也可以使用第三方模块,今天学习一下如何使用自己定义的模块
在同级目录下定义两个js文件
第一个:custom1.js
"use strict";
function hello() {
console.log("Hello world!!");
}
//将自定义的一个函数hello抛出到外部
module.exports = hello;
第二个:custom2.js
"use strict";
//引入上面抛出的模块,注意这里引入的是上面的文件名
let test = require("./custom1");
//使用custom1抛出的hello函数
test();
总结:
1.在Node中使用require引入模块(不管是自定义的还是内置的)
2.使用test存储custom1.js文件使用module.exports抛出的内容
3.module.exports抛出的内容可以是任何东西(字符串、函数、对象等)
4.引入自定义模块的时候需要在前面加上"./",否则可能会报错,不加"./"它会去内置模块查找该模块
5.在Node中引入的内置模块更多的是使用"."来使用这个模块中的内容的,其实我们也是可以这样使用,只需要在抛出内容的时候抛出的是对象即可
例如我们再custom1.js文件使用module.exports抛出东西的后可以这样:
module.exports = {"hello" : hello};
这样我们就可以在custom2.js的文件中使用 test.hello() 的形式来使用hello方法
Node.js 自定义模块的更多相关文章
- node.js之模块
node.js之模块 1.自定义模块的设置 加载自定义模块利用require: eg: require('./custom_module.js') 2.从模块外部访问模块内的成员 2.1使用expor ...
- Node.js之模块机制
> 文章原创于公众号:程序猿周先森.本平台不定时更新,喜欢我的文章,欢迎关注我的微信公众号.  (计数)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1259 解决:686 题目描述: Finding all occurrences of a pattern in a text is a p ...
- ssh key 生成
1.设置好git的name和email $ git config --global user.name "姓名" $ git config --global user.email ...
- Hadoop实战-Flume之Source interceptor(十一)(2017-05-16 22:40)
a1.sources = r1 a1.sinks = k1 a1.channels = c1 # Describe/configure the source a1.sources.r1.type = ...
- Leetcode:remove_duplicates_from_sorted_list
一. 题目 给定一个排好序的链表,删除全部反复的节点,使每个节点都仅仅出现一次 比如: Given 1->1->2, return 1->2. Given 1->1-& ...
- Unable to start adb server: adb server version (32) doesn't match this client (39); killing...
关于Android studio 连接不上adb问题,有人说重启机器,有人说重启工具,也有人说adb kill-server.然后我都尝试过依然没有解决.通过各种查询.最终成功的解决!!! adb n ...
- SCAU 还有两个东西 —— 异或
竞赛题 F 还有两个东西 Time Limit:400MS Memory Limit:65535K 题型: 编程题 语言: 无限制 描述 给出n( n >= 2 )个整数,其中有 2 个 ...
- mysql general log开启
#先查看当前状态 mysql> show variables like 'general%'; +------------------+----------------------------- ...
- HTML5 Canvas 时钟
1. [图片] QQ截图20120712130049.png 2. [代码][HTML]代码 <!DOCTYPE html><html lang="en" &g ...