Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.node 初识 #8

Open
Zijue opened this issue Feb 17, 2021 · 0 comments
Open

1.node 初识 #8

Zijue opened this issue Feb 17, 2021 · 0 comments
Labels

Comments

@Zijue
Copy link
Owner

Zijue commented Feb 17, 2021

node 是什么?能做什么?

  • node 不是语言,它是一个让 js 可以运行在服务端的一个运行时(内置模块提供文件读写、操作系统 api)
  • js 语言组成部分 BOM、DOM、ECMAScript;node 中只包含 ECMAScript + 模块
  • node 静态服务不如 nginx,高并发不如 go;但是 node 做中间层十分合适(解决跨域问题,ssr 的实现,写工具及(egg/nest)后台项目)
  • 高并发(单线程:js 中主线程是单线程,使用回调的方式处理高并发)

非阻塞异步 I/O 特性

  • node 使用 异步非阻塞 + 单线程(主线程)
  • node 中实现了 异步非阻塞库 libuv(多线程来实现的),核心是异步

多线程的优点:可以做压缩合并等大量计算相关的(cpu 密集型);node 适合 I/O 密集型(web 应用的常见)

阻塞非阻塞、同步异步

  • 阻塞与非阻塞针对是调用方,同步异步针对是被调用方
  • 常见的 I/O 模型:同步阻塞、异步非阻塞(当完成任务后以事件的形式通知)

node 的多版本管理

  • mac 可以使用 nvm(brew install nvm)切换 node 的版本
  • node 默认安装会送一个 npm node package manage

默认 node 中的 this 是谁?

  • 在 node 执行环境中的 this 指向的是全局变量 global
  • node + 文件名的方式来执行;在文件中 this 指向的是 module.exports,默认是 {},原因是 CommonJS 规范所有的代码写到文件中,文件内部会自带一个函数,这个函数执行的时候改变了 this
@Zijue Zijue added the node.js label Feb 17, 2021
@Zijue Zijue changed the title 1.node的概念 1.node.js 初识 Feb 17, 2021
@Zijue Zijue changed the title 1.node.js 初识 1.node 初识 Feb 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant