introduction to nodejs

24
Node.js

Upload: techpartyuic

Post on 22-Jun-2015

612 views

Category:

Documents


8 download

DESCRIPTION

Introduction to NodeJS, 1st TechParty@UIC, Zelong Liang

TRANSCRIPT

Page 1: Introduction to NodeJS

Node.js

Page 2: Introduction to NodeJS

Who am I?

• @龙mol_才不是小眼睛

Page 3: Introduction to NodeJS

Introduction

• Node.js是什么?

• Node.js的优势与特点是什么?

– 什么是V8引擎

– 什么是非阻塞模式

• Node.js能解决什么问题?

• Node.js实践?

Page 4: Introduction to NodeJS

Node.js是什么

• Node.JS 是资深 C 程序员 Ryan Dahl的作品,依据 Google 著名的开源 JavaScript 引擎 V8 来进行二次开发的 Web I/O 服务器

• 轻量级WebServer

• 脱离了浏览器在后端运行的Javascript代码

• 运行坏境、库

Page 5: Introduction to NodeJS

优势与特点

• 单进程(single-thread)• 异步非阻塞(non-blocking)• 事件驱动(event-based)• V8引擎

• 性能很好

• 第三方模块丰富

• 速度、灵活

Page 6: Introduction to NodeJS

V8引擎

• V8是一个由丹麦Google开发的开源JavaScript引擎,用于Google Chrome中。

• V8在执行之前将JavaScript编译成了机器码,而非字节码或是直译它,以此提升效能。并且使用了如内联缓存(inline caching)等方法来提高性能。

• 有了这些功能,JavaScript程序与V8引擎的速度媲美二进制编译。

Page 7: Introduction to NodeJS

非阻塞模式

• 在非阻塞模式下利用socket事件的消息机制,Server端与Client端之间的通信处于异步状态。

• 通常需要从CSocket类派生一个新类,派生新类的目的是重载socket事件的消息函数,然后在socket事件的消息函数中添入合适的代码以完成Client端与Server端之间的通信。

• 与阻塞模式相比,非阻塞模式无需创建一个新线程。

Page 8: Introduction to NodeJS

性能

• Python:Tornado• Go• Java:Netty• Nodejs

Page 9: Introduction to NodeJS
Page 10: Introduction to NodeJS
Page 11: Introduction to NodeJS
Page 12: Introduction to NodeJS
Page 13: Introduction to NodeJS
Page 14: Introduction to NodeJS
Page 15: Introduction to NodeJS

第三方模块

• 官方共收集列出1152个第三方模块!

– 涵盖了Web、Database、XML、Web Sockets & Ajax、API clients、TCP/IP、CSS Enginess等等,几乎涉及网络开发中需要的功能模块。

管理工具:npm管理着4427+模块,想用哪个用哪个,妈妈在也不用担心我没有模块用!

Page 16: Introduction to NodeJS

Node.js能解决什么问题

• Node 公开宣称的目标是 “旨在提供一种简单的构建可伸缩网络程序的方法 ”。

Page 17: Introduction to NodeJS

E.g.• 在 Java™ 和 PHP 这类语言中,每个连接都会生成一个新线程,每个新线程可能需要 2 MB 的配套内存。在一个拥有 8 GB RAM 的系统上,理论上最大的并发连接数量是 4,000 个用户。随着您的客户群的增长,如果希望您的 Web 应用程序支持更多用户,那么,您必须添加更多服务器。当然,这会增加服务器成本、流量成本和人工成本等成本。

• 瓶颈是:服务器能够处理的并发连接的最大数量。

Page 18: Introduction to NodeJS

A

• 解决这个问题的方法是:更改连接到服务器的方式。每个连接发射一个在 Node 引擎的进程中运行的事件,而不是为每个连接生成一个新的 OS 线程(并为其分配一些配套内存)。Node 声称它绝不会死锁,因为它根本不允许使用锁,它不会直接阻塞 I/O 调用。Node 还宣称,运行它的服务器能支持数万个并发连接。

Page 19: Introduction to NodeJS

Node.js实践

• 不受限制地访问文件和网络资源

• 前后端代码复用

• 解决跨域访问的问题

Page 20: Introduction to NodeJS

常规Web应用

Page 21: Introduction to NodeJS

实时性强的网络应用

Page 22: Introduction to NodeJS

实时性强的网络应用

Page 23: Introduction to NodeJS

Q&A

Page 24: Introduction to NodeJS

Thanks!