Download presentation
Presentation is loading. Please wait.
1
Node.js - What is Node.js? -
Sep. 2014 Youn-Hee Han
2
What is Node.js? A JavaScript runtime environment running Google Chrome’s V8 engine a.k.a. a server-side solution for JS Runs over the command line Designed for high concurrency Without threads or new processes Making it really fast Never blocks, not even for I/O Created by Ryan Dahl starting in 2009 Node.js is a platform (is not a framework) Express is a framework for Node.js 참고
3
What is Node.js? Goal is to provide an easy way to build scalable network programs. It makes communication between client and server happen in same language (JavaScript) Node is a platform for writing JavaScript applications outside web browsers. There is no DOM built into Node, nor any other browser capability. Node is actually programmed in C. Because of the nature of C, Node can perform amazingly well when dealing with networking and OS system calls
4
Asynchronous and evented
In the browser Asynchronous Ajax request using XMLHttpRequest I/O doesn’t block execution Callback function The code was not written like this I/O blocks execution until finished $.post('/resource.json', function (data) { console.log(data); }); // script execution continues var data = $.post('/resource.json'); console.log(data);
5
Concurrency: Event Loop
Instead of threads, Node uses an event loop with a stack Alleviates overhead of context switching Event loop as a language construct instead of as a library It process each request as events E.g., HTTP server doesn’t wait for the IO operation to complete while it can handle other request at the same time. To avoid blocking, it makes use of the event driven nature of JavaScript by attaching callbacks to I/O requests
6
Concurrency: Event Loop
An example of non-blocking I/O in the browser
7
Concurrency: Event Loop
Thread vs. Event Loop Threads Event Loop Lock application / request with listener-workers threads only one thread, which repeatedly fetches an event Using incoming-request model Using queue and then processes it multithreaded server might block the request which might involve multiple events manually saves state and then goes on to process the next event Using context switching no contention and no context switches
8
Number of request per second
Node.js/HTTP vs. Apache Node.js/HTTP It's fast It can handle tons of concurrent requests It's written in JavaScript (which means you can use the same code server side and client side) Platform Number of request per second PHP ( via Apache) 3187,27 Static ( via Apache ) 2966,51 Node.js 5569,30
9
NGNIX vs. Apache NGNIX An HTTP server useing an event loop with asynchronous I/O
10
DIRTy applications DIRTy application
Data-intensive real-time application Node.js is suitable for DIRTy application Node.js includes a core set of modules for many types of network and file I/O. Building blocks for I/O-based applications HTTP, HTTPS, filesystem (POSIX), UDP, and NET (TCP). The core is intentionally small, low-level, and uncomplicated. Third-party modules build upon these blocks to offer greater abstractions for common problems.
11
Example 1. Simple Async App.
Node.js install REFL 101 Node.js app using filesystem (fs) module var fs = require('fs'); fs.readFile('./resource.json', function (er, data) { console.log(data); })
12
Example 2. Hello World HTTP Server
An HTTP server simply responding to any request with “Hello World” Same server to make the request event explicit var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(3000); console.log('Server running at var http = require('http'); var server = http.createServer(); server.on('request', function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }) server.listen(3000); console.log('Server running at
13
Example 3. Streaming data
Stream is data distributed over time By bringing data in chunk by chunk, the developer handles the data as it comes in instead of waiting for it all to arrive before acting. var stream = fs.createReadStream('./resource.json') stream.on('data', function (chunk) { console.log(chunk) }) stream.on('end', function () { console.log('finished')
14
Example 4. HTTP Streaming
An HTTP server streaming an image to a client Node.js provides these DIRTy-by-default approach across multiple platforms var http = require('http'); var fs = require('fs'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'image/png'}); fs.createReadStream('./image.png').pipe(res); }).listen(3000); console.log('Server running at
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.