210 likes | 500 Views
Node.js - What is Node.js? -. Sep. 2014 Youn-Hee Han http://link.koreatech.ac.kr. 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
E N D
Node.js- What is Node.js? - Sep. 2014 Youn-Hee Han http://link.koreatech.ac.kr
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 • http://sbrich.tistory.com/395참고
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
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);
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
Concurrency: Event Loop • An example of non-blocking I/O in the browser
Concurrency: Event Loop • Thread vs. Event Loop
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)
NGNIX vs. Apache • NGNIX • An HTTP server useing an event loop with asynchronous I/O
DIRTy applications • DIRTy application • Data-intensive real-timeapplication • 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.
Example 1. Simple Async App. • Node.js install • http://nodejs.org/ • REFL 101 • http://thinkonweb.com/?p=423 • Node.js app using filesystem (fs) module var fs = require('fs'); fs.readFile('./resource.json', function (er, data) { console.log(data); })
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 http://localhost:3000/'); • 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 http://localhost:3000/');
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') })
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 http://localhost:3000/');