Skip to main content

Nodejs

目录

常用模块 fs children_process cluster http event-loop websocket node 如何部署 负载均衡 Cluster pm2 node 如何处理高并发 node 线上出现故障,流量如何分配 框架 koa 中间件 express 设计思想 中间件系统 puppeteer npm 包安装机制 commonJS 实现原理

ENOSPC Node.js error

If you are running Debian, RedHat, or another similar Linux distribution, run the following in a terminal:

echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p

If you are running ArchLinux, run the following command instead (see here for why):

echo fs.inotify.max_user_watches=524288 | sudo tee /etc/sysctl.d/40-max-user-watches.conf && sudo sysctl --system

优点

  • RESTful API
  • 单线程
    • 无法利用多核CPU
    • 错误会引起整个应用退出,需要考虑应用健壮性
    • 大量计算占用 CPU 导致无法继续调用异步 IO
  • 事件轮询机制
  • 异步 I/O
  • V8 虚拟机
  • 事件驱动
  • 跨平台

应用场景

  • IO 密集型
    • 面向网络擅长并行IO,有效组织更多硬件资源,提供更好的服务。
  • 面向服务的架构
  • RESTful API
  • Ajax 请求应用
  • 分布式应用

不适用的场景

  • 实时性要求很高的场景
  • 计算密集型系统
  • 需要单一进程控制大内存的场景

由于 V8 引擎限制,在 32-bit 下有 1GB最大内存尺寸的限制,在 64-bit 下是 1.7GB。虽然 Nodejs 框架的 Buffer 分配可以不超过此限制,但是也会带来垃圾回收机制上的性能退化。

参考