资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

nodejs渐入佳境[3]-require导入模块-创新互联

require 导入其他文件

require可以执行其他文件的内容。

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:空间域名、虚拟空间、营销软件、网站建设、岳麓网站维护、网站推广。

新建文件: nodes.js:

1
console.log('start nodes.js');

app.js:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//打印字符串
console.log('Start app.');

const nodes = require('./nodes.js')
//导入node自带modules,fs文件操作
const fs = require('fs');
//os系统操作
const os = require('os');
//获取系统名字
var user = os.userInfo();

//添加字符串到回调函数中,并执行回调函数。出错就会报错,不出错就会打印出'The "data to append" was appended to file!'
fs.appendFile('greetings.txt',`Hello ${user.username}`,(err) => {
 if (err) throw err;
 console.log('The "data to append" was appended to file!');
}
);

打开控制台,在当前目录下输入:

1
> node app.js

输出字符串

1
2
3
Start app.
start nodes.js
The "data to append" was appended to file!

require 导入属性

nodes.js:

1
2
3
console.log('start nodes.js');

module.exports.age = 25;

app.js:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//打印字符串
console.log('Start app.');

const nodes = require('./nodes.js')
//导入node自带modules,fs文件操作
const fs = require('fs');
//os系统操作
const os = require('os');
//获取系统名字
var user = os.userInfo();

//添加字符串到回调函数中,并执行回调函数。出错就会报错,不出错就会打印出'The "data to append" was appended to file!'
fs.appendFile('greetings.txt',`Hello ${user.username} age ${nodes.age}`,(err) => {
 if (err) throw err;
 console.log('The "data to append" was appended to file!');
}
);

打开控制台,在当前目录下输入:

1
> node app.js

文件中存储:Hello jackson age 25

require 导入函数

nodes.js:

1
2
3
4
5
6
console.log('start nodes.js');

module.exports.addNote = ()=>{
 console.log('addNode');
 return 'New Node';
};

app.js:

1
2
3
4
5
6
console.log('Start app.');

const nodes = require('./nodes.js')

const res = nodes.addNote();
console.log(res);

打开控制台,在当前目录下输入:

1
> node app.js

输出字符串

1
2
3
4
Start app.
start nodes.js
addNode
New Node

require 导入带参函数

nodes.js:

1
2
3
4
5
6
7
8
9
10
11
console.log('start nodes.js');

module.exports.add = (a,b)=>{

 return a+b;
};

module.exports.addNote = ()=>{
 console.log('addNode');
 return 'New Node';
};

app.js:

1
2
3
4
5
6
7
//打印字符串
console.log('Start app.');

const nodes = require('./nodes.js')

const res = nodes.add(1,2);
console.log(res);

打开控制台,在当前目录下输入:

1
> node app.js

输出字符串

1
2
3
Start app.
start nodes.js
3
  • 本文链接: https://dreamerjonson.com/2018/11/13/node-3-require/

  • 版权声明:本博客所有文章除特别声明外,均采用 CC BY 4.0 CN协议 许可协议。转载请注明出处!

nodejs渐入佳境[3]-require导入模块

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网页名称:nodejs渐入佳境[3]-require导入模块-创新互联
当前路径:http://cdkjz.cn/article/csejcj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220