Node.js Guides(General)

这里是Node.js官网中docs Guides部分的翻看笔记,中间夹杂一些个人理解,如果能够真正深入理解官网文中的内容,那么对Node.js的使用将会有一个质的提升,原始地址点这里

  • 1.1、Getting Started Guide 入门指南

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    const http = require('http');
    const hostname = '127.0.0.1';
    const port = 3000;
    const server = http.createServer((req, res) => {
    console.log('jiayufeng');
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');
    res.end('Hello World\n');
    });
    server.listen(port, hostname, () => {
    console.log(`Server running at http://${hostname}:${port}/`);
    });
  • 1.2、Debugging - Getting Started 调试

    • 启动时在node后加–inspect进入调试模式
    • http://127.0.0.1:9229/json/list地址可以获取到程序基本信息
    • 调试器可以获得node执行器的全部权限
    • 贼难用,比直接命令行难用多了
  • 1.3、Easy profiling for Node.js Applications 应用分析

    • 有很多三方工具可以分析node应用,v8内置了性能分析器
    • 使用node –prof app.js运行程序,会在程序运行目录中生成一个tick文件
    • 要明白tick文件的内容,需要使用tick文件处理器,node –prof-process isolate-0xnnnnnnnnnnnn-v8.log > processed.txt
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      [Summary]:
      ticks total nonlib name
      79 0.2% 0.2% JavaScript
      36703 97.2% 99.2% C++
      7 0.0% 0.0% GC
      767 2.0% Shared libraries
      215 0.6% Unaccounted
      有97%的工作在c++中完成,当我们更加关注输入输出时,就主要看c++的运行效果

      [C++]:
      ticks total nonlib name
      19557 51.8% 52.9% node::crypto::PBKDF2(v8::FunctionCallbackInfo<v8::Value> const&)
      4510 11.9% 12.2% _sha1_block_data_order
      3165 8.4% 8.6% _malloc_zone_malloc
      有51.8%的时间被一个名为PBKDF2的程序占用
  • 1.4、Dockerizing a Node.js web app docker化node应用

  • 1.5、Migrating to safe Buffer constructors 迁移到安全的buffer构造函数
    • 这部分主要说以前版本的Node程序如何将buffer代码改为比较安全的代码
◀        
        ▶