global
//node中的全局变量是global, 浏览器中的全局变量是window
> global.console
Console {
log: [Function: bound consoleCall],
info: [Function: bound consoleCall],
warn: [Function: bound consoleCall],
error: [Function: bound consoleCall],
dir: [Function: bound consoleCall],
time: [Function: bound consoleCall],
timeEnd: [Function: bound consoleCall],
trace: [Function: bound consoleCall],
assert: [Function: bound consoleCall],
clear: [Function: bound consoleCall],
count: [Function: bound consoleCall],
countReset: [Function: bound countReset],
Console: [Function: Console],
debug: [Function: debug],
dirxml: [Function: dirxml],
table: [Function: table],
group: [Function: group],
groupCollapsed: [Function: groupCollapsed],
groupEnd: [Function: groupEnd],
markTimeline: [Function: markTimeline],
profile: [Function: profile],
profileEnd: [Function: profileEnd],
timeline: [Function: timeline],
timelineEnd: [Function: timelineEnd],
timeStamp: [Function: timeStamp],
[Symbol(counts)]: Map {} }
process
> process === global.process;
true
> process.version;
'v8.4.0'
> process.platform;
'darwin'
> process.arch;
'x64'
> process.cwd(); //返回当前工作目录
'/Users/michael'
> process.chdir('/private/tmp'); // 切换当前工作目录
undefined
> process.cwd();
'/private/tmp'
//在下一次事件响应中执行代码,可以调用process.nextTick()
process.nextTick(function () {
console.log('nextTick callback!');
});
console.log('nextTick was set!');
//程序即将退出时的回调函数:
process.on('exit', function (code) {
console.log('about to exit with code: ' + code);
});
//判断js的执行环境
if (typeof(window) === 'undefined') {
console.log('node.js');
} else {
console.log('browser');
}
fs
//fs是指文件系统模块,一般包括同步和异步处理文件,node.js中一般是通过异步处理文件
'use strict';
var fs = require('fs'); //引入模块
fs.readFile('./demo.js', 'utf-8', function (err, data) {
if (err) {
console.log(err);
} else {
console.log(data);
}
});
stream
stream是Node.js提供的又一个仅在服务区端可用的模块,目的是支持“流”这种数据结构
http
用程序并不直接和HTTP协议打交道,而是操作http模块提供的request和response对象
crypto
crypto模块的目的是为了提供通用的加密和哈希算法