{
getPrice,
(2). 模块B导入,并使用
//1. 导入模块A的信息(commonjs规范)
var moduleA = require('./01-moduleA');
//2. 调用相关方法
console.log(moduleA.Add1(1,2));
console.log(moduleA.Add22(1,2,3));
console.log(moduleA.sum1);
console.log(moduleA.sum2);
console.log(`9+6=${moduleA.Fk1(9,6)}`);
console.log(`name=${moduleA.name}`);
二. 读写文件、路径问题
1. 读写问题
//1.引入文件系统
const fs=require('fs');
//2. 读取文件
fs.readFile('./Temp/test1.txt', 'utf8', (err, doc) => {
// 如果文件读取出错err 是一个对象 包含错误信息
// 如果文件读取正确 err是 null
// doc 是文件读取的结果
console.log(err);
console.log(doc);
//3. 写入文件
fs.writeFile('./Temp/test2.txt', 'lmr111111111', err => {
if (err != null) {
console.log(err);
return;
console.log('文件内容写入成功');
2. 路径问题
var path = require('path');
//1.使用__dirname获取当前文件所在的绝对路径
//e:\06-架构之路\06-前端框架\03-NodeJs专题\02-code\nodejs基础
console.log(__dirname);
//__filename直接定位到当前文件
//e:\06-架构之路\06-前端框架\03-NodeJs专题\02-code\nodejs基础\04-相关路径.js
console.log(__filename);
//2. 路径拼接
const finalPath1 = path.join('public', 'uploads','avatar');
const finalPath2=path.join(__dirname,'01.js');
console.log(`finalPath1=${finalPath1}`);
console.log(`finalPath2=${finalPath2}`);
运行结果:
作 者 : Yaopengfei(姚鹏飞)
博客地址 : http://www.cnblogs.com/yaopengfei/
声 明1 : 如有错误,欢迎讨论,请勿谩骂^_^。
声 明2 : 原创博客请在转载时保留原文链接或在文章开头加上本人博客地址,否则保留追究法律责任的权利。