1、CommonJS
我们只是在js文件中写下console.log('This is a test.');这句代码,node.js执行该js文件时:
( function(exports, require, module, __filename, __dirname) { console.log('This is a test.'); } );
- 每个文件是一个模块,有自己的作用域
- 在模块内部module变量代表模块本身
- module.exports属性代表模块对外接口
2、require特性
- module被加载时执行,加载后缓存
- 一旦出现某个模块被循环加载,就只会输出已经执行的部分,还未执行的部分不输出。开发中要避免两个模块相互加载。
3、node.js将全局属性和方法挂到global上
- CommonJs
- Buffer、process、console
- timer(setTimeout、setImmediate等)
4、process进程
- node启动相关一些参数:argv、argv0、execArgv、execPath
argv, argv0: 命令行参数,argv0就是argv数组的第一个值
const { argv, argv0, execArgv, execPath } = process; argv.forEach(item => { console.log(item); });
execArgv: 执行参数 ,比如--inspect
const { argv, argv0, execArgv, execPath } = process; argv.forEach(item => { console.log(item); }); console.log(argv0); console.log(execArgv);
环境变量 env
const { env } = process;
console.log(env);
结果:
{ '#envTSLOGSHELLEXT10724': '351103568', '#envTSLOGsss10724': '80627808', '#envTSLOGTSLOG10724': '80616288', ALLUSERSPROFILE: 'C:\ProgramData', APPDATA: 'C:\Users\oy\AppData\Roaming', CommonProgramFiles: 'C:\Program Files\Common Files', 'CommonProgramFiles(x86)': 'C:\Program Files (x86)\Common Files', CommonProgramW6432: 'C:\Program Files\Common Files', COMPUTERNAME: 'LAPTOP-SHVUJ9TS', ComSpec: 'C:\WINDOWS\system32\cmd.exe', DriverData: 'C:\Windows\System32\Drivers\DriverData', FPS_BROWSER_APP_PROFILE_STRING: 'Internet Explorer', FPS_BROWSER_USER_PROFILE_STRING: 'Default', HOMEDRIVE: 'C:', HOMEPATH: '\Users\oy', JAVA_HOME: 'D:\JavaDevelop\jdk1.8.0_111', LOCALAPPDATA: 'C:\Users\oy\AppData\Local', LOGONSERVER: '\\LAPTOP-SHVUJ9TS', MYSQL_HOME: 'D:\JavaDevelop\mysql-5.1.69-winx64', NUMBER_OF_PROCESSORS: '8', OneDrive: 'C:\Users\oy\OneDrive', OneDriveConsumer: 'C:\Users\oy\OneDrive', OS: 'Windows_NT', Path: 'C:\Users\oy\AppData\Roaming\npm;D:\DevTools\php-5.6.40-Win32-VC11-x64;D:\JavaDevelop\mysql-5.1.69-winx64\bin;D:\JavaDevelop\jdk1.8.0_111\bin;C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;D:\DevTools\nodejs\;C:\Users\oy\AppData\Local\Microsoft\WindowsApps;C:\Users\oy\AppData\Roaming\npm;', PATHEXT: '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.CPL', PHP_HOME: 'D:\DevTools\php-5.6.40-Win32-VC11-x64', PROCESSOR_ARCHITECTURE: 'AMD64', PROCESSOR_IDENTIFIER: 'AMD64 Family 23 Model 17 Stepping 0, AuthenticAMD', PROCESSOR_LEVEL: '23', PROCESSOR_REVISION: '1100', ProgramData: 'C:\ProgramData', ProgramFiles: 'C:\Program Files', 'ProgramFiles(x86)': 'C:\Program Files (x86)', ProgramW6432: 'C:\Program Files', PSModulePath: 'C:\Users\oy\Documents\WindowsPowerShell\Modules;C:\Program Files\WindowsPowerShell\Modules;C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules', PUBLIC: 'C:\Users\Public', SESSIONNAME: 'Console', SystemDrive: 'C:', SystemRoot: 'C:\WINDOWS', TEMP: 'C:\Users\oy\AppData\Local\Temp', TMP: 'C:\Users\oy\AppData\Local\Temp', USERDOMAIN: 'LAPTOP-SHVUJ9TS', USERDOMAIN_ROAMINGPROFILE: 'LAPTOP-SHVUJ9TS', USERNAME: 'oy', USERPROFILE: 'C:\Users\oy', windir: 'C:\WINDOWS' }
5、setImmediate
// 下个队列 setImmediate(() => { console.log('setImmediate'); }); setTimeout(() => { console.log('setTimeout'); }); // 当前队列的队尾 process.nextTick(() => { console.log('nextTick'); }); // 结果: nextTick setTimeout setImmediate