SlideShare una empresa de Scribd logo
1 de 13
Descargar para leer sin conexión
深入浅出NodeJS
恐龙王国事业部 孙政华
特性简介
• single-threaded
• event-driven
• chrome's V8 JavaScript Engine
• non-blocking I/O model
single-threaded
• Apache is multi-threaded
– thread per request(Apache MPM worker)
– process(Apache MPM prefork)
更多的并发连接,带来更多的线程和内存开销
• Nginx & NodeJS is single-threaded
– but event-based(使其不会阻塞)
– but V8 is multi-threaded(worker threads)
没有了创建额外线程/进程的开销
*Apache 2.4正式支持了Event MPM
event-driven
– 单线程不能并行执行代码,但事件驱动和异
步,使其不会阻塞
– libeio:基于事件的完全异步的I/O库,他用多
线程来实现IO访问再回调nodejs主线程。
– libev:全功能、高性能循环事件处理框架,和
libevent很像,但没有libevent的局限性和
bugs。
V8 JavaScript Engine
• V8不是线程安全的,所有的V8操作必须从
单个V8线程中执行,这意味着你不可以从
其他线程创建objects, integers, strings, etc.如
果创建的话,可能会种族灭绝哦(内存及
NodeJS会崩溃)。所以NodeJS的机制是在
worker thread(eio线程)中执行阻塞的任
务,任务执行完回调主JS线程。
• Valgrind是一个提供程序调试及性能分析的
工具集,可以查找内存泄露哦。
垃圾回收机制
• 使用了V8的GC:V8回收再利用不再被进程使用的对象内存,
以保证快速的分配对象,较短的垃圾回收停顿,和没有内存碎
片。这意味着V8会:
– 在一个垃圾回收周期堵塞/停止程序的执行
– 在大多数垃圾回收周期只回收对象堆的一部分,这能最小化
因堵塞/停止程序而产生的冲突
– 总是必须精确知道所有对象在内催中的指针,这可以避免错
误识别对象指针,否则会内存泄露。
– 在V8中,对象堆被分成两个段:新空间(创建新对象),
旧空间(在每个垃圾回收周期,将对象放入)。如果对象在
某个垃圾回收周期中被移动了,V8会更新该对象的所有指
针。
– nodejs 可以使用–max-old-space-size设置旧内存空间
non-blocking I/O model
• 与PHP的不同
npm
• npm是一个NodeJS的包管理器,可以使用它
安装及发布基于NodeJS的程序。
• 安装npm:curl http://npmjs.org/install.sh |
sh
• 安装包:npm install xxxxxx
• 发布包:npm init / adduser / publish
一些有用的包
• memcache
• redis
• policyfile
• socket.io
• express
• jade
• eventproxy
• nodemon
• ......
代码示例
全局对象
• global:全局命名空间对象
• process:进程对象(argv,exit,
uncaughtException)
• require:用来加载模块
• module:只是一个对当前对象的引用。特
别说明:module.exports和exports是一
样一样地。
更多:http://nodejs.org/docs/latest/api
一些问题
• 10个场景(地图),每个场景大概300-
500个玩家,玩家随时随地会在地图里面走
动,还可能买新装备穿在身上或者更换装
备,怎么处理呢?
• 需要获取用户信息,装备信息,背包信
息,怎样比较好的获得呢?(注意获取动
作都是异步的)eventproxy,async
谢谢大家

Más contenido relacionado

La actualidad más candente

Angular 4 新手入門攻略完全制霸
Angular 4 新手入門攻略完全制霸Angular 4 新手入門攻略完全制霸
Angular 4 新手入門攻略完全制霸Will Huang
 
Azure Container Service 使用 DC / OS 管理 docker 容器
Azure Container Service 使用 DC / OS 管理 docker 容器Azure Container Service 使用 DC / OS 管理 docker 容器
Azure Container Service 使用 DC / OS 管理 docker 容器Ch Rick
 
Android线程简介
Android线程简介Android线程简介
Android线程简介朋 王
 
雲端運算期末報告v2
雲端運算期末報告v2雲端運算期末報告v2
雲端運算期末報告v2岱霖 黎
 
雲端運算期末報告
雲端運算期末報告雲端運算期末報告
雲端運算期末報告岱霖 黎
 
Windows Container 101: dotNET, Container, Kubernetes
Windows Container 101: dotNET, Container, KubernetesWindows Container 101: dotNET, Container, Kubernetes
Windows Container 101: dotNET, Container, KubernetesWill Huang
 
Kubernetes use-ceph
Kubernetes use-cephKubernetes use-ceph
Kubernetes use-cephYang Guanjun
 
打造輕量化手機網站
打造輕量化手機網站打造輕量化手機網站
打造輕量化手機網站Rei Ayanami
 
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!Will Huang
 
Share module
Share moduleShare module
Share moduledpf2e
 
实时Web的前世今生未来
实时Web的前世今生未来实时Web的前世今生未来
实时Web的前世今生未来RolfZhang
 
如何攻破大型副本 大型專案的開發與團隊合作經驗 聊天室
如何攻破大型副本   大型專案的開發與團隊合作經驗 聊天室如何攻破大型副本   大型專案的開發與團隊合作經驗 聊天室
如何攻破大型副本 大型專案的開發與團隊合作經驗 聊天室Study4TW
 
Track2 -刘继伟--openstack in gamewave
Track2 -刘继伟--openstack in gamewaveTrack2 -刘继伟--openstack in gamewave
Track2 -刘继伟--openstack in gamewaveOpenCity Community
 
Nodejs & NAE
Nodejs & NAENodejs & NAE
Nodejs & NAEq3boy
 
NODEjs Lesson1
NODEjs Lesson1NODEjs Lesson1
NODEjs Lesson13dmodeldiy
 
使用 Visual Studio Code 建構 JavaScript 應用程式
使用 Visual Studio Code 建構 JavaScript 應用程式使用 Visual Studio Code 建構 JavaScript 應用程式
使用 Visual Studio Code 建構 JavaScript 應用程式Will Huang
 
全新 Windows Server 2019 容器技術 及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)
全新 Windows Server 2019 容器技術及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)全新 Windows Server 2019 容器技術及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)
全新 Windows Server 2019 容器技術 及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)Will Huang
 

La actualidad más candente (20)

Node分享 展烨
Node分享 展烨Node分享 展烨
Node分享 展烨
 
Angular 4 新手入門攻略完全制霸
Angular 4 新手入門攻略完全制霸Angular 4 新手入門攻略完全制霸
Angular 4 新手入門攻略完全制霸
 
Azure Container Service 使用 DC / OS 管理 docker 容器
Azure Container Service 使用 DC / OS 管理 docker 容器Azure Container Service 使用 DC / OS 管理 docker 容器
Azure Container Service 使用 DC / OS 管理 docker 容器
 
Android线程简介
Android线程简介Android线程简介
Android线程简介
 
雲端運算期末報告v2
雲端運算期末報告v2雲端運算期末報告v2
雲端運算期末報告v2
 
雲端運算期末報告
雲端運算期末報告雲端運算期末報告
雲端運算期末報告
 
SASS入門實作
SASS入門實作SASS入門實作
SASS入門實作
 
Windows Container 101: dotNET, Container, Kubernetes
Windows Container 101: dotNET, Container, KubernetesWindows Container 101: dotNET, Container, Kubernetes
Windows Container 101: dotNET, Container, Kubernetes
 
Kubernetes use-ceph
Kubernetes use-cephKubernetes use-ceph
Kubernetes use-ceph
 
Mesos intro
Mesos introMesos intro
Mesos intro
 
打造輕量化手機網站
打造輕量化手機網站打造輕量化手機網站
打造輕量化手機網站
 
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!
 
Share module
Share moduleShare module
Share module
 
实时Web的前世今生未来
实时Web的前世今生未来实时Web的前世今生未来
实时Web的前世今生未来
 
如何攻破大型副本 大型專案的開發與團隊合作經驗 聊天室
如何攻破大型副本   大型專案的開發與團隊合作經驗 聊天室如何攻破大型副本   大型專案的開發與團隊合作經驗 聊天室
如何攻破大型副本 大型專案的開發與團隊合作經驗 聊天室
 
Track2 -刘继伟--openstack in gamewave
Track2 -刘继伟--openstack in gamewaveTrack2 -刘继伟--openstack in gamewave
Track2 -刘继伟--openstack in gamewave
 
Nodejs & NAE
Nodejs & NAENodejs & NAE
Nodejs & NAE
 
NODEjs Lesson1
NODEjs Lesson1NODEjs Lesson1
NODEjs Lesson1
 
使用 Visual Studio Code 建構 JavaScript 應用程式
使用 Visual Studio Code 建構 JavaScript 應用程式使用 Visual Studio Code 建構 JavaScript 應用程式
使用 Visual Studio Code 建構 JavaScript 應用程式
 
全新 Windows Server 2019 容器技術 及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)
全新 Windows Server 2019 容器技術及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)全新 Windows Server 2019 容器技術及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)
全新 Windows Server 2019 容器技術 及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)
 

Destacado

Mitubisi ibennto
Mitubisi ibenntoMitubisi ibennto
Mitubisi ibenntosmileasahi
 
Facebook y el jueves
Facebook y el juevesFacebook y el jueves
Facebook y el juevesBondiatauli
 
Cадчикова карина
Cадчикова каринаCадчикова карина
Cадчикова каринаKarisha96
 
Cадчикова карина
Cадчикова каринаCадчикова карина
Cадчикова каринаKarisha96
 
Sistema Respiratorio
Sistema RespiratorioSistema Respiratorio
Sistema Respiratoriodancp30
 
Trabajar las competencias en el aula
Trabajar las competencias en el aulaTrabajar las competencias en el aula
Trabajar las competencias en el aulaRoman Pelaez Forne
 
Power guineu 1[1]
Power guineu 1[1]Power guineu 1[1]
Power guineu 1[1]mvilagr2
 
Godwillnevergiveuponyou 110723153743-phpapp01-1
Godwillnevergiveuponyou 110723153743-phpapp01-1Godwillnevergiveuponyou 110723153743-phpapp01-1
Godwillnevergiveuponyou 110723153743-phpapp01-1bwiegel
 
Dissertação 2006 USP
Dissertação 2006 USPDissertação 2006 USP
Dissertação 2006 USPdeboradavid
 
Power guineu 1[1]
Power guineu 1[1]Power guineu 1[1]
Power guineu 1[1]mvilagr2
 

Destacado (20)

Press 2011
Press 2011Press 2011
Press 2011
 
Mitubisi ibennto
Mitubisi ibenntoMitubisi ibennto
Mitubisi ibennto
 
Los deportes
Los deportesLos deportes
Los deportes
 
Los deportes
Los deportesLos deportes
Los deportes
 
Los deportes
Los deportesLos deportes
Los deportes
 
Facebook y el jueves
Facebook y el juevesFacebook y el jueves
Facebook y el jueves
 
El+árbol+..
El+árbol+..El+árbol+..
El+árbol+..
 
Cадчикова карина
Cадчикова каринаCадчикова карина
Cадчикова карина
 
Cадчикова карина
Cадчикова каринаCадчикова карина
Cадчикова карина
 
Sistema Respiratorio
Sistema RespiratorioSistema Respiratorio
Sistema Respiratorio
 
Torhoutvoorstelling
TorhoutvoorstellingTorhoutvoorstelling
Torhoutvoorstelling
 
Energia solar
Energia solarEnergia solar
Energia solar
 
Telenoika
TelenoikaTelenoika
Telenoika
 
Tema 6
Tema 6Tema 6
Tema 6
 
Trabajar las competencias en el aula
Trabajar las competencias en el aulaTrabajar las competencias en el aula
Trabajar las competencias en el aula
 
Power guineu 1[1]
Power guineu 1[1]Power guineu 1[1]
Power guineu 1[1]
 
Godwillnevergiveuponyou 110723153743-phpapp01-1
Godwillnevergiveuponyou 110723153743-phpapp01-1Godwillnevergiveuponyou 110723153743-phpapp01-1
Godwillnevergiveuponyou 110723153743-phpapp01-1
 
Adivinanza
AdivinanzaAdivinanza
Adivinanza
 
Dissertação 2006 USP
Dissertação 2006 USPDissertação 2006 USP
Dissertação 2006 USP
 
Power guineu 1[1]
Power guineu 1[1]Power guineu 1[1]
Power guineu 1[1]
 

Similar a 深入浅出NodeJS

lua & ngx_lua 的介绍与应用
lua & ngx_lua 的介绍与应用lua & ngx_lua 的介绍与应用
lua & ngx_lua 的介绍与应用hugo
 
Introduction to NodeJS
Introduction to NodeJSIntroduction to NodeJS
Introduction to NodeJSTechParty@UIC
 
Node.js中间件 connect模块深入浅出
Node.js中间件 connect模块深入浅出Node.js中间件 connect模块深入浅出
Node.js中间件 connect模块深入浅出Eric Xiao
 
Node.js长连接开发实践
Node.js长连接开发实践Node.js长连接开发实践
Node.js长连接开发实践longhao
 
Node js feat pegasus
Node js feat pegasusNode js feat pegasus
Node js feat pegasuscnfi
 
Node js实践
Node js实践Node js实践
Node js实践myzykj
 
Nodejs异步原理和缺陷 - 赵成
Nodejs异步原理和缺陷 - 赵成Nodejs异步原理和缺陷 - 赵成
Nodejs异步原理和缺陷 - 赵成Jackson Tian
 
非常靠谱 Html 5
非常靠谱 Html 5 非常靠谱 Html 5
非常靠谱 Html 5 Tony Deng
 
Nodejs异步的原理和缺陷
Nodejs异步的原理和缺陷Nodejs异步的原理和缺陷
Nodejs异步的原理和缺陷mysqlops
 
Tech.days Taiwan AZR305
Tech.days Taiwan AZR305 Tech.days Taiwan AZR305
Tech.days Taiwan AZR305 Jeff Chu
 
NodeJS基礎教學&簡介
NodeJS基礎教學&簡介NodeJS基礎教學&簡介
NodeJS基礎教學&簡介GO LL
 
构建基于Lamp的网站架构
构建基于Lamp的网站架构构建基于Lamp的网站架构
构建基于Lamp的网站架构Cosey Lee
 
Asp.net 5 新功能與變革
Asp.net 5 新功能與變革Asp.net 5 新功能與變革
Asp.net 5 新功能與變革Gelis Wu
 
Nodejs introduce - using Socket.io
Nodejs introduce - using Socket.ioNodejs introduce - using Socket.io
Nodejs introduce - using Socket.ioCaesar Chi
 
Nodejs介绍
Nodejs介绍Nodejs介绍
Nodejs介绍myzykj
 
Notes of jcip
Notes of jcipNotes of jcip
Notes of jcipDai Jun
 
Open whisk makes your cloud serverless 20180320
Open whisk makes your cloud serverless 20180320Open whisk makes your cloud serverless 20180320
Open whisk makes your cloud serverless 20180320Ying Chun Guo
 
Real World ASP.NET MVC
Real World ASP.NET MVCReal World ASP.NET MVC
Real World ASP.NET MVCjeffz
 

Similar a 深入浅出NodeJS (20)

lua & ngx_lua 的介绍与应用
lua & ngx_lua 的介绍与应用lua & ngx_lua 的介绍与应用
lua & ngx_lua 的介绍与应用
 
Introduction to NodeJS
Introduction to NodeJSIntroduction to NodeJS
Introduction to NodeJS
 
Node.js中间件 connect模块深入浅出
Node.js中间件 connect模块深入浅出Node.js中间件 connect模块深入浅出
Node.js中间件 connect模块深入浅出
 
Node.js长连接开发实践
Node.js长连接开发实践Node.js长连接开发实践
Node.js长连接开发实践
 
Node js feat pegasus
Node js feat pegasusNode js feat pegasus
Node js feat pegasus
 
Node js实践
Node js实践Node js实践
Node js实践
 
Nodejs异步原理和缺陷 - 赵成
Nodejs异步原理和缺陷 - 赵成Nodejs异步原理和缺陷 - 赵成
Nodejs异步原理和缺陷 - 赵成
 
非常靠谱 Html 5
非常靠谱 Html 5 非常靠谱 Html 5
非常靠谱 Html 5
 
Nodejs异步的原理和缺陷
Nodejs异步的原理和缺陷Nodejs异步的原理和缺陷
Nodejs异步的原理和缺陷
 
Tech.days Taiwan AZR305
Tech.days Taiwan AZR305 Tech.days Taiwan AZR305
Tech.days Taiwan AZR305
 
NodeJS基礎教學&簡介
NodeJS基礎教學&簡介NodeJS基礎教學&簡介
NodeJS基礎教學&簡介
 
构建基于Lamp的网站架构
构建基于Lamp的网站架构构建基于Lamp的网站架构
构建基于Lamp的网站架构
 
Asp.net 5 新功能與變革
Asp.net 5 新功能與變革Asp.net 5 新功能與變革
Asp.net 5 新功能與變革
 
Nodejs introduce - using Socket.io
Nodejs introduce - using Socket.ioNodejs introduce - using Socket.io
Nodejs introduce - using Socket.io
 
Nodejs介绍
Nodejs介绍Nodejs介绍
Nodejs介绍
 
OPM
OPMOPM
OPM
 
Notes of jcip
Notes of jcipNotes of jcip
Notes of jcip
 
Open whisk makes your cloud serverless 20180320
Open whisk makes your cloud serverless 20180320Open whisk makes your cloud serverless 20180320
Open whisk makes your cloud serverless 20180320
 
Real World ASP.NET MVC
Real World ASP.NET MVCReal World ASP.NET MVC
Real World ASP.NET MVC
 
摘星
摘星摘星
摘星
 

深入浅出NodeJS