nodejs学习第4篇——process对象

这一片主要介绍process对象。话不多说了,打开webstrom,建立项目,建立一个process.js.然后在里面写入如下代码:

console.log(process);

运行process.js文件。结果如下:

"D:\java\WebStorm 2017.3.1\bin\runnerw.exe" D:\java\node\node.exe E:\javaRepository\webstrom\test\global\process.js
process {
  title: 'D:\\java\\WebStorm 2017.3.1\\bin\\runnerw.exe',
  version: 'v8.9.3',
  moduleLoadList: 
   [ 'Binding contextify',
     'Binding natives',
     'Binding config',
     'NativeModule events',
     'Binding async_wrap',
     'Binding icu',
     'NativeModule util',
     'NativeModule internal/errors',
     'NativeModule internal/encoding',
     'NativeModule internal/util',
     'Binding util',
     'Binding constants',
     'NativeModule internal/util/types',
     'Binding buffer',
     'NativeModule buffer',
     'NativeModule internal/buffer',
     'Binding uv',
     'NativeModule internal/process',
     'NativeModule internal/process/warning',
     'NativeModule internal/process/next_tick',
     'NativeModule async_hooks',
     'NativeModule internal/process/promises',
     'NativeModule internal/process/stdio',
     'Binding performance',
     'NativeModule perf_hooks',
     'NativeModule internal/linkedlist',
     'NativeModule internal/inspector_async_hook',
     'Binding inspector',
     'NativeModule timers',
     'Binding timer_wrap',
     'NativeModule assert',
     'NativeModule module',
     'NativeModule internal/module',
     'NativeModule internal/url',
     'NativeModule internal/querystring',
     'NativeModule querystring',
     'Binding url',
     'NativeModule vm',
     'NativeModule fs',
     'NativeModule path',
     'Binding fs',
     'NativeModule stream',
     'NativeModule internal/streams/legacy',
     'NativeModule _stream_readable',
     'NativeModule internal/streams/BufferList',
     'NativeModule internal/streams/destroy',
     'NativeModule _stream_writable',
     'NativeModule _stream_duplex',
     'NativeModule _stream_transform',
     'NativeModule _stream_passthrough',
     'Binding fs_event_wrap',
     'NativeModule internal/fs',
     'NativeModule internal/loader/Loader',
     'NativeModule internal/loader/ModuleWrap',
     'Internal Binding module_wrap',
     'NativeModule internal/loader/ModuleMap',
     'NativeModule internal/loader/ModuleJob',
     'NativeModule internal/safe_globals',
     'NativeModule internal/loader/ModuleRequest',
     'NativeModule url',
     'NativeModule internal/loader/search',
     'NativeModule console',
     'Binding tty_wrap',
     'NativeModule net',
     'NativeModule internal/net',
     'Binding cares_wrap',
     'Binding tcp_wrap',
     'Binding pipe_wrap',
     'Binding stream_wrap',
     'NativeModule dns' ],
  versions: { http_parser: '2.7.0',
  node: '8.9.3',
  v8: '6.1.534.48',
  uv: '1.15.0',
  zlib: '1.2.11',
  ares: '1.10.1-DEV',
  modules: '57',
  nghttp2: '1.25.0',
  openssl: '1.0.2n',
  icu: '59.1',
  unicode: '9.0',
  cldr: '31.0.1',
  tz: '2017b' },
  arch: 'x64',
  platform: 'win32',
  release: 
   { name: 'node',
     lts: 'Carbon',
     sourceUrl: 'https://nodejs.org/download/release/v8.9.3/node-v8.9.3.tar.gz',
     headersUrl: 'https://nodejs.org/download/release/v8.9.3/node-v8.9.3-headers.tar.gz',
     libUrl: 'https://nodejs.org/download/release/v8.9.3/win-x64/node.lib' },
  argv: 
   [ 'D:\\java\\node\\node.exe',
     'E:\\javaRepository\\webstrom\\test\\global\\process.js' ],
  execArgv: [],
  env: 
   { ALLUSERSPROFILE: 'C:\\ProgramData',
     APPDATA: 'C:\\Users\\Administrator\\AppData\\Roaming',
     CommonProgramFiles: 'C:\\Program Files\\Common Files',
     'CommonProgramFiles(x86)': 'C:\\Program Files (x86)\\Common Files',
     CommonProgramW6432: 'C:\\Program Files\\Common Files',
     COMPUTERNAME: 'PC-20170714RVBS',
     ComSpec: 'C:\\Windows\\system32\\cmd.exe',
     DEBUG_COLORS: 'true',
     ELECTRON_NO_ATTACH_CONSOLE: 'true',
     ERLANG_HOME: 'D:\\java\\erl9.2',
     FORCE_COLOR: 'true',
     FP_NO_HOST_CHECK: 'NO',
     HOMEDRIVE: 'C:',
     HOMEPATH: '\\Users\\Administrator',
     JAVA_HOME: 'D:\\java\\java1.8.0',
     LOCALAPPDATA: 'C:\\Users\\Administrator\\AppData\\Local',
     LOGONSERVER: '\\\\PC-20170714RVBS',
     MOCHA_COLORS: '1',
     MYSQL_HOME: 'D:\\java\\mysql-5.7.13-winx64',
     NODE_PATH: 'D:\\java\\node\\node_modules',
     npm_config_color: 'always',
     NUMBER_OF_PROCESSORS: '4',
     OS: 'Windows_NT',
     Path: 'C:\\ProgramData\\Oracle\\Java\\javapath;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;D:\\java\\java1.8.0bin;D:\\java\\mysql-5.7.13-winx64bin;D:\\java\\Git\\cmd;C:\\Program Files (x86)\\Microsoft SQL Server\\100\\Tools\\Binn\\;C:\\Program Files\\Microsoft SQL Server\\100\\Tools\\Binn\\;C:\\Program Files\\Microsoft SQL Server\\100\\DTS\\Binn\\;C:\\Program Files (x86)\\Microsoft SQL Server\\100\\Tools\\Binn\\VSShell\\Common7\\IDE\\;C:\\Program Files (x86)\\Microsoft Visual Studio 9.0\\Common7\\IDE\\PrivateAssemblies\\;C:\\Program Files (x86)\\Microsoft SQL Server\\100\\DTS\\Binn\\;D:\\java\\node;E:\\javaRepository\\node\\node_global',
     PATHEXT: '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC',
     PROCESSOR_ARCHITECTURE: 'AMD64',
     PROCESSOR_IDENTIFIER: 'Intel64 Family 6 Model 42 Stepping 7, GenuineIntel',
     PROCESSOR_LEVEL: '6',
     PROCESSOR_REVISION: '2a07',
     ProgramData: 'C:\\ProgramData',
     ProgramFiles: 'C:\\Program Files',
     'ProgramFiles(x86)': 'C:\\Program Files (x86)',
     ProgramW6432: 'C:\\Program Files',
     PSModulePath: 'C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\Modules\\',
     PUBLIC: 'C:\\Users\\Public',
     SESSIONNAME: 'Console',
     SystemDrive: 'C:',
     SystemRoot: 'C:\\Windows',
     TEMP: 'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp',
     TMP: 'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp',
     USERDOMAIN: 'PC-20170714RVBS',
     USERNAME: 'Administrator',
     USERPROFILE: 'C:\\Users\\Administrator',
     windir: 'C:\\Windows',
     windows_tracing_flags: '3',
     windows_tracing_logfile: 'C:\\BVTBin\\Tests\\installpackage\\csilogfile.log' },
  pid: 8188,
  features: 
   { debug: false,
     uv: true,
     ipv6: true,
     tls_npn: true,
     tls_alpn: true,
     tls_sni: true,
     tls_ocsp: true,
     tls: true },
  _needImmediateCallback: false,
  execPath: 'D:\\java\\node\\node.exe',
  debugPort: 9229,
  _startProfilerIdleNotifier: [Function: _startProfilerIdleNotifier],
  _stopProfilerIdleNotifier: [Function: _stopProfilerIdleNotifier],
  _getActiveRequests: [Function: _getActiveRequests],
  _getActiveHandles: [Function: _getActiveHandles],
  reallyExit: [Function: reallyExit],
  abort: [Function: abort],
  chdir: [Function: chdir],
  cwd: [Function: cwd],
  umask: [Function: umask],
  _kill: [Function: _kill],
  _debugProcess: [Function: _debugProcess],
  _debugPause: [Function: _debugPause],
  _debugEnd: [Function: _debugEnd],
  hrtime: [Function: hrtime],
  cpuUsage: [Function: cpuUsage],
  dlopen: [Function: dlopen],
  uptime: [Function: uptime],
  memoryUsage: [Function: memoryUsage],
  binding: [Function: binding],
  _linkedBinding: [Function: _linkedBinding],
  _setupDomainUse: [Function: _setupDomainUse],
  _events: 
   { warning: [Function],
     newListener: [Function],
     removeListener: [Function] },
  _rawDebug: [Function],
  _eventsCount: 3,
  domain: null,
  _maxListeners: undefined,
  _fatalException: [Function],
  _exiting: false,
  assert: [Function],
  config: 
   { target_defaults: 
      { cflags: [],
        default_configuration: 'Release',
        defines: [],
        include_dirs: [],
        libraries: [] },
     variables: 
      { asan: 0,
        coverage: false,
        debug_devtools: 'node',
        debug_http2: false,
        debug_nghttp2: false,
        force_dynamic_crt: 0,
        host_arch: 'x64',
        icu_data_file: 'icudt59l.dat',
        icu_data_in: '..\\..\\deps/icu-small\\source/data/in\\icudt59l.dat',
        icu_endianness: 'l',
        icu_gyp_path: 'tools/icu/icu-generic.gyp',
        icu_locales: 'en,root',
        icu_path: 'deps/icu-small',
        icu_small: true,
        icu_ver_major: '59',
        node_byteorder: 'little',
        node_enable_d8: false,
        node_enable_v8_vtunejit: false,
        node_install_npm: true,
        node_module_version: 57,
        node_no_browser_globals: false,
        node_prefix: '/usr/local',
        node_release_urlbase: 'https://nodejs.org/download/release/',
        node_shared: false,
        node_shared_cares: false,
        node_shared_http_parser: false,
        node_shared_libuv: false,
        node_shared_openssl: false,
        node_shared_zlib: false,
        node_tag: '',
        node_use_bundled_v8: true,
        node_use_dtrace: false,
        node_use_etw: true,
        node_use_lttng: false,
        node_use_openssl: true,
        node_use_perfctr: true,
        node_use_v8_platform: true,
        node_without_node_options: false,
        openssl_fips: '',
        openssl_no_asm: 0,
        shlib_suffix: 'so.57',
        target_arch: 'x64',
        v8_enable_gdbjit: 0,
        v8_enable_i18n_support: 1,
        v8_enable_inspector: 1,
        v8_no_strict_aliasing: 1,
        v8_optimized_debug: 0,
        v8_promise_internal_field_count: 1,
        v8_random_seed: 0,
        v8_trace_maps: 0,
        v8_use_snapshot: true,
        want_separate_host_toolset: 0 } },
  emitWarning: [Function],
  nextTick: [Function: nextTick],
  _tickCallback: [Function: _tickCallback],
  _tickDomainCallback: [Function: _tickDomainCallback],
  stdout: [Getter],
  stderr: [Getter],
  stdin: [Getter],
  openStdin: [Function],
  exit: [Function],
  kill: [Function],
  argv0: 'D:\\java\\node\\node.exe',
  mainModule: 
   Module {
     id: '.',
     exports: {},
     parent: null,
     filename: 'E:\\javaRepository\\webstrom\\test\\global\\process.js',
     loaded: false,
     children: [],
     paths: 
      [ 'E:\\javaRepository\\webstrom\\test\\global\\node_modules',
        'E:\\javaRepository\\webstrom\\test\\node_modules',
        'E:\\javaRepository\\webstrom\\node_modules',
        'E:\\javaRepository\\node_modules',
        'E:\\node_modules' ] } }

Process finished with exit code 0

丫的,一大堆,都不想粘贴出来。还是去看官网的教程说明吧:
首先:官网上解释了process对象:
process 对象是一个 global (全局变量),提供有关信息,控制当前 Node.js 进程。作为一个对象,它对于 Node.js 应用程序始终是可用的,故无需使用 require()。
其次我们看一看process对象的属性及方法吧,由于太多我就不一个个看了。我就列举几个我认为有点用的吧。
1.事件(event):
beforeExit:当Node.js的事件循环数组已经为空,并且没有额外的工作被添加进来,事件’beforeExit’会被触发。 正常情况下,如果没有额外的工作被添加到事件循环数组,Node.js进程会结束。但是如果’beforeExit’事件绑定的监听器的回调函数中,含有一个可以进行异步调用的操作,那么Node.js进程会继续运行。
disconnect:如果Node.js进程是由IPC channel的方式创建的(see the Child Process and Cluster documentation),当IPC channel关闭时,会触发’disconnect’事件。
exit:两种情况下’exit’事件会被触发:1)显式调用process.exit()方法,使得Node.js进程即将结束;2)Node.js事件循环数组中不再有额外的工作,使得Node.js进程即将结束。
message:如果Node.js进程是由IPC channel的方式创建的(see the Child Process, and Cluster documentation),当子进程收到父进程的的消息时(消息通过childprocess.send()发送), 会触发’message’事件。
uncaughtException:如果Javascript未捕获的异常,沿着代码调用路径反向传递回event loop,会触发’uncaughtException’事件。 Node.js默认情况下会将这些异常堆栈打印到stderr 然后进程退出。 为’uncaughtException’事件增加监听器会覆盖上述默认行为。
好了就列举这几个吧,其他的自己去官网看吧。
2.属性:
argv:process.argv 属性返回一个数组,返回的是当前运行该文件的程序以及当前文件位置。
env:返回的是当前设备的系统信息
pid:就是任务管理器上面的pid。
然后还有一大堆乱七八糟的东西,就等以后用上了再来介绍吧。
接下来讲点process的一个小demo,方便我们理解。建立一个process2.js,在里面写上如下代码:

/**
 * 用于监听用户的输入数据(默认情况下输入流是关闭的,要监听输入流,需要先开启输入流)
 */
process.stdin.resume();
var a;var b;
process.stdout.write("请输入a的值:");
process.stdin.on('data',function(chunk){
    if(!a){
        a=Number(chunk);
        process.stdout.write("请输入b的值:");
    }else {
        b=Number(chunk);
        process.stdout.write("结果是:"+(a+b));
    }
});

运行起来,效果如下
nodejs学习第4篇——process对象_第1张图片
还是很好玩吧,就跟java中的scanner类差不多。
接着我们再在文件中输入下面代码:

console.log(process===global.process);

运行结果为true,说明process对象与global.process是一样的,加上前面的exports与module.exports这里就有两对了呢。

你可能感兴趣的:(nodejs学习)