对于前端工程师的要求是需要了解一下node.js的,所以啊,我这里先存放一个学习笔记,欢迎各位大神指正哦
一.模块
在Node环境中,一个.js文件就称之为一个模块(module)。使用模块的好处就是大大提高了代码的可维护性,复用性,减少代码冗余。
1>模块导出,使用module.exports = moduleName;
例如 hello.js
'use strict';
var s = 'Hello';
function greet(name) {
console.log(s + ', ' + name + '!');
}
module.exports = greet;
2>模块的引入使用require('模块的相对路径')
例如:在统计目录下的main.js中引入hello.js
var greet = require('./hello'); // 不要忘了写相对目录!
二、CommonJS规范
1.深入了解模块的原理
JavaScript语言本身并没有一种模块机制来保证不同模块可以使用相同的变量名。那Node.js实现“模块”功能的奥妙就在于JavaScript是一种函数式编程语言,它支持闭包。如果我们把一段JavaScript代码用一个函数包装起来,这段代码的所有“全局”变量就变成了函数内部的局部变量。
例如
Node.js加载了hello.js后,它可以把代码包装一下,变成这样执行:
(function () {
// 读取的hello.js代码:
var s = 'Hello';
var name = 'world';
console.log(s + ' ' + name + '!');
// hello.js代码结束
})();
这样就实现了模块的隔离。模块之间即使是用相同的变量,引入到一个文件的时候也不会冲突。
2.module.exports vs exports(两种导出模块的方法)
方法一:对module.exports赋值:
// hello.js
function hello() {
console.log('Hello, world!');
}
function greet(name) {
console.log('Hello, ' + name + '!');
}
module.exports = {
hello: hello,
greet: greet
};
方法二:直接使用exports:
// hello.js
function hello() {
console.log('Hello, world!');
}
function greet(name) {
console.log('Hello, ' + name + '!');
}
function hello() {
console.log('Hello, world!');
}
exports.hello = hello;
exports.greet = greet;
注意:但是你不可以直接对exports赋值
就像这样子,不可以的哇,这就是然并卵,嘻嘻
// 代码可以执行,但是模块并没有输出任何变量:
exports = {
hello: hello,
greet: greet
};
强烈建议使用module.exports = xxx
的方式来输出模块变量,这样,你只需要记忆一种方法,年纪大了记不住了可不要记混了,嘻嘻
三、基本模块
1.global
进入Node.js交互环境,输入global.console
得到全局方法
2.process
process也是Node.js提供的一个对象,它代表当前Node.js进程。通过process对象可以拿到许多有用信息:
eg:
> process === global.process;
true
> process.version;
'v5.2.0'
> process.platform;
'darwin'
> process.arch;
'x64'
> process.cwd(); //返回当前工作目录
'/Users/michael'
> process.chdir('/private/tmp'); // 切换当前工作目录
undefined
> process.cwd();
'/private/tmp'
在下一次事件响应中执行代码,可以调用process.nextTick()
,此函数不是立刻执行,而是要等到下一次事件循环。
Node.js进程本身的事件就由process对象来处理。如果我们响应exit事件,就可以在程序即将退出时执行某个回调函数:
// 程序即将退出时的回调函数:
process.on('exit', function (code) {
console.log('about to exit with code: ' + code);
});
!!!!判断JavaScript执行环境
if (typeof(window) === 'undefined') {
console.log('node.js');
} else {
console.log('browser');
}
四、fs模块
Node.js内置的fs模块就是文件系统模块,负责读写文件。fs模块同时提供了异步和同步的方法
(1)异步读取一个文本文件的代码如下
'use strict';
var fs = require('fs');
fs.readFile('sample.txt', 'utf-8', function (err, data) {
if (err) {
console.log(err);
} else {
console.log(data);
}
});
第一个参数代表错误信息,第二个参数代表结果。
注:当读取二进制文件时,不传入文件编码时,回调函数的data参数将返回一个Buffer对象。在Node.js中,Buffer对象就是一个包含零个或任意个字节的数组(注意和Array不同)。
Buffer对象可以和String作转换,例如,把一个Buffer对象转换成String:
// Buffer -> String
var text = data.toString('utf-8');
console.log(text);
或者把一个String转换成Buffer:
// String -> Buffer
var buf = Buffer.from(text, 'utf-8');
console.log(buf);
(2)用fs模块同步读取一个文本文件的代码如下:
'use strict';
var fs = require('fs');
var data = fs.readFileSync('sample.txt', 'utf-8');
console.log(data);
原异步调用的回调函数的data被函数直接返回,函数名需要改为readFileSync
,其它参数不变
如果同步读取文件发生错误,则需要用try...catch
捕获该错误
try {
var data = fs.readFileSync('sample.txt', 'utf-8');
console.log(data);
} catch (err) {
// 出错了
}
写文件
将数据写入文件是通过fs.writeFile()实现的:
'use strict';
var fs = require('fs');
var data = 'Hello, Node.js';
fs.writeFile('output.txt', data, function (err) {
if (err) {
console.log(err);
} else {
console.log('ok.');
}
});
writeFile()的参数依次为文件名、数据和回调函数。如果传入的数据是String,默认按UTF-8编码写入文本文件,如果传入的参数是Buffer,则写入的是二进制文件。回调函数由于只关心成功与否,因此只需要一个err参数。
和readFile()
类似,writeFile()
也有一个同步方法,叫writeFileSync()
: