Node 核心和 Node eventLoop

2020-09-01  本文已影响0人  育儿与心理

Node 核心和 Node eventLoop

Node 是什么

Node 解决了什么问题

Web服务器,瓶颈在于用户的并发量 (多线程 同步) 只要多个人需要操作同一个资源必须通过锁

node 比较适合web应用场景 返回文件 文件读写

Node核心特点

同步异步 阻塞非阻塞(针对的点不一样的)

Node 中的全局对象

this 和 module.exports 是 === 的

  console.log(this); // {} this 不是 global
  console.log(this === global); // false
  console.log(this === module.exports); // true

arguments

  console.log(arguments);
  // [Arguments] {
  //   '0': {},
  //   '1': [Function: require] {
  //     resolve: [Function: resolve] { paths: [Function: paths] },
  //     main: Module {
  //       id: '.',
  //       path: '/Users/hiraku/myself/architecture-product/node/4.global',
  //       exports: {},
  //       parent: null,
  //       filename: '/Users/hiraku/myself/architecture-product/node/4.global/index.js',
  //       loaded: false,
  //       children: [],
  //       paths: [Array]
  //     },
  //     extensions: [Object: null prototype] {
  //       '.js': [Function],
  //       '.json': [Function],
  //       '.node': [Function],
  //       '.mjs': [Function]
  //     },
  //     cache: [Object: null prototype] {
  //       '/Users/hiraku/myself/architecture-product/node/4.global/index.js': [Module]
  //     }
  //   },
  //   '2': Module {
  //     id: '.',
  //     path: '/Users/hiraku/myself/architecture-product/node/4.global',
  //     exports: {},
  //     parent: null,
  //     filename: '/Users/hiraku/myself/architecture-product/node/4.global/index.js',
  //     loaded: false,
  //     children: [],
  //     paths: [
  //       '/Users/hiraku/myself/architecture-product/node/4.global/node_modules',
  //       '/Users/hiraku/myself/architecture-product/node/node_modules',
  //       '/Users/hiraku/myself/architecture-product/node_modules',
  //       '/Users/hiraku/myself/node_modules',
  //       '/Users/hiraku/node_modules',
  //       '/Users/node_modules',
  //       '/node_modules'
  //     ]
  //   },
  //   '3': '/Users/hiraku/myself/architecture-product/node/4.global/index.js',
  //   '4': '/Users/hiraku/myself/architecture-product/node/4.global'
  // }

global key

  console.log(Object.keys(global));
  // [
  //   'global',
  //   'clearInterval',
  //   'clearTimeout',
  //   'setInterval',
  //   'setTimeout',
  //   'queueMicrotask',
  //   'clearImmediate',
  //   'setImmediate'
  // ]

process 进程

  console.log(Object.keys(process));
  // ['version','arch','platform','release','_rawDebug','moduleLoadList','binding','_linkedBinding','_events','_eventsCount','_maxListeners','domain','_exiting','config','abort','umask','chdir','cwd','_debugProcess','_debugEnd','_startProfilerIdleNotifier','_stopProfilerIdleNotifier','dlopen','uptime','_getActiveRequests','_getActiveHandles','reallyExit','_kill','hrtime','cpuUsage','resourceUsage','memoryUsage','kill','exit','getuid','geteuid','getgid','getegid','getgroups','initgroups','setgroups','setegid','seteuid','setgid','setuid','stdout','stderr','stdin','openStdin','allowedNodeEnvironmentFlags','assert','features','_fatalException','setUncaughtExceptionCaptureCallback','hasUncaughtExceptionCaptureCallback','emitWarning','nextTick','_tickCallback','env','title','argv','execArgv','pid','ppid','execPath','debugPort','argv0','_preload_modules','mainModule'
  // node index.js --port 3000
  console.log(process.argv);
  // 1. 当前 node 的执行命令文件
  // 2. 当前执行的文件是谁 node + 文件执行时 可以传递参数 这些参数会放到 数组的第三项
  // 3. 解析用户传递的参数
  // [ '/Users/hiraku/.nvm/versions/node/v11.10.0/bin/node',
  //   '/Users/hiraku/myself/architecture-product/node/global/index.js',
  //   '--port',
  //   '3000' ]
  const argvObj = process.argv.slice(2).reduce((memo, current, index, arr) => {
    if(current.startsWith('--')) {
      memo[current.slice(2)] = arr[index + 1];
    }
    return memo;
  }, {});
  console.log(argvObj);
  console.log(process.platform, 'platform'); // darwin
  // win32 darwin
  const cmd = require('commander');
  cmd.name('node global');
  cmd.usage('index.js');
  cmd.version('1.0.0');
  cmd.option('-p,--port <v>', 'please set you prot ');
  cmd.option('-c,--config <v>', 'please set  you config file ');
  cmd.command('create').action(() => { // 运行时会执行此方法
      console.log('创建项目');
  });
  cmd.on('--help', function () {
      console.log('\r\nRun command')
      console.log('\r\n  node global -p 3000')
  });
  const r = cmd.parse(process.argv);
  console.log(r);
  console.log(process.env);
  console.log(process.cwd());

  // Usage: node global index.js

  // Options:
  //   -V, --version    output the version number
  //   -p,--port <v>    please set you prot
  //   -c,--config <v>  please set  you config file
  //   -h, --help       display help for command

  // Commands:
  //   create
  //   help [command]   display help for command

  // Run command

  //   node global -p 3000
  console.log(process.cwd()); // /Users/hiraku/myself/architecture-product/node/4.global
// window set命令 export 命令  => cross-env
console.log(process.env) // 当前进程的环境变量 会用他来区分各种环境
// cross-env env=development && node xxxx

Node 中的实现的微任务

  process.nextTick(() => {
    console.log(1);
    process.nextTick(() => {
      console.log(2);
      process.nextTick(() => {
        console.log(3);
      });
    });
  });
  Promise.resolve().then(() => {
    console.log('promise')
  });

  // 1 2 3 promise
  setImmediate(() => { // 立即
    console.log('setImmediate'); // node 中的宏任务
  });
  setTimeout(() => {
    console.log('setTimeout');
  }, 0);
  // setTimeout
  // setImmediate
  setImmediate(() => {
    console.log('setImmediate1');
    process.nextTick(() => {
      Promise.resolve().then(() => {
        console.log('promise1');
      });
    });
  });
  setImmediate(() => {
    console.log('setImmediate2');
    Promise.resolve().then(() => {
      console.log('promise2');
    });
    process.nextTick(() => {
      console.log('nextTick2');
    });
  });
  process.nextTick(() => {
    console.log('nextTick1');
  });

  // nextTick1
  // setImmediate1
  // promise1
  // setImmediate2
  // nextTick2
  // promise2
企业微信20200901-190600@2x.png
上一篇下一篇

猜你喜欢

热点阅读