Exploring the Future Potential of AI-Enabled Smartphone Processors
A slightly advanced introduction to node.js
1. An Introduction to Node.JS Sudar Muthu (@sudarmuthu) Research Engineer Yahoo Labs http://sudarmuthu.com
2. Agenda What Node.JS is What Node.JS is not Why Node.JS? Using Node.JS As a interactive shell As a Server As a Client Common Modules Terminologies Questions
3. What is Node.JS Provides Evented, non-blocking I/O Built on Google’s V8 JavaScript Programming Environment Supports C/C++ based addons Supports CommonJS Module format Is fast .. infact very fast.
4. What is Node.JS Similar to EventMachine in Ruby Twisted in Python But provides Evented IO as part of the language construct itself and not as a library.
5. Node.JS is not … Ruby on Rails Django Codeigniter Node.JS is bare bone and the community are making stuff like Express, connect etc.
6. Why Node.JS? Code like this var result = db.query("select.."); // use result either blocks the entire process or implies multiple execution stacks (threads).
7. Why Node.JS? But a line of code like this db.query("select..", function (result) { // use result }); allows the program to return to the event loop immediately. No more unnecessary threads.
8. Demo of Callback // execute the callback after 2 seconds setTimeout(function () { console.log("World!"); }, 2000); // print in console console.log("Hello"); https://github.com/sudar/jsfoo/blob/master/callback.js
9. Using Node.JS Install Node.JS Install NPM Install other modules you want by doing npm install <module_name> You are good to go
10. Node.JS – as an interactive shell Similar to Python’s shell $> node > 3 + 1 4 > true != false true >.help >.exit
11. Node.JS – As a server var http = require('http'); // require the http module // create a server http.createServer(function (req, res) { // call this function when a request is received res.writeHead(200, { 'Content-Type': 'text/plain' }); // send this as part of the response res.end('Hello World'); }).listen(1337, "127.0.0.1"); // listen on port 1337 // debug information console.log('Server running at http://127.0.0.1:1337/'); https://github.com/sudar/jsfoo/blob/master/http-server.js
12. Node.JS – As a client var http = require('http'); // require the needed modules // make the request object var request = http.request({ 'host': 'sudarmuthu.com', 'port': 80, 'path': '/', 'method': 'GET' }); // assign callbacks request.on('response', function (response) { console.log('Response Status Code: ' + response.statusCode); response.on('data', function (data) { console.log('Body: ' + data); }); }); https://github.com/sudar/jsfoo/blob/master/http-client.js
13. Core Modules Processes Filesystem Networking Utilities The entire list can be found at http://nodejs.org/docs/v0.4.12/api/
14. Core Modules - Processes Node allows you to analyze your process and manage external process Available Modules process child_process Code samples: http://github.com/sudar/jsfoo/process.js
15. Core Modules - Filesystem Low level API to manipulate files Available Modules fs path Code Samples: http://github.com/sudar/jsfoo/filesystem.js
16. Core Modules - Networking Available Modules net dgram http tls https dns Code Samples: https://github.com/sudar/jsfoo/blob/master/dns.js
18. Node.JS is useful for.. Writing highly concurrent server applications Sharing application logic between server and client Peer-to-peer web applications using websockets