Node.js is an open-source JavaScript runtime environment built on Chrome's V8 JavaScript engine. It allows JavaScript to be run on the server-side. Some key points:
- First released in 2009, it uses asynchronous and event-driven programming to handle concurrent connections efficiently.
- It is built on JavaScript and uses the V8 engine, allowing for fast development using a single language across front-end and back-end.
- It is designed for data-intensive real-time applications due to its asynchronous and event-driven nature, which makes it efficient for handling concurrent connections.
3. History
• First published for Linux use in 2009
• Original author: Ryan Dahl
• npm, a package manager for Node.js libraries,
was introduced in 2011
• In June 2011, Microsoft partnered with Joyent
to create a native Windows version of Node.js
• Used by Groupon, SAP, LinkedIn, Microsoft,
Yahoo!, Walmart, Rakuten, PayPal, Voxer,
GoDaddy …
4.
5. What is Node.js
• Node is a platform for JavaScript applications
• Node uses V8, the virtual machine that
powers Google Chrome
• Node is
– Built on JavaScript
– Evented and asynchronous
– DIRTy by default
6. Built on JavaScript
• JavaScript is the world’s most popular
programming language
• Developers can write web applications (client
& server) in one language
• JSON is a very popular data interchange
format today and is native to JavaScript
• JavaScript is the language used in various
NoSQL databases
7. Asynchronous and evented
• Node is event-driven (uses an event loop) and
non-blocking when handling I/O (uses
asynchronous I/O)
• Example blocking code from PHP
• In Node, I/O is almost always performed
outside of the main event loop, allowing the
server to stay efficient and responsive
$result = mysql_query('SELECT * FROM myTable');
print_r($result);
8.
9. Designed for data-intensive real-
time applications
• DIRT : data-intensive real-time
• https://browserling.com/
10. • Node tries to keep consistency between the browser
and the server by re-implementing common host
objects:
– Timer API (for example, setTimeout)
– Console API (for example, console.log)
• Also includes a core set of modules for many types of
network and file I/O.
– HTTP, TLS, HTTPS, filesystem (POSIX), Datagram (UDP), and
NET (TCP).
• The core is intentionally small, low-level, and
uncomplicated, including just the building blocks for
I/O-based applications.
– Ex: HTTP parser consisting of roughly 1,500 lines of
optimized C code.
11. Hello World HTTP server
var http = require('http');
var server = http.createServer();
server.on('request', function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Worldn');
});
server.listen(3000);
console.log('Server running at http://localhost:3000/');
13. Create Modules
• Modules can either be single files or
directories containing one or more files
• Create a file that defines properties on the
exports object with any kind of data, such
as strings, objects, and functions
15. test-currency.js
var currency = require('./currency');
console.log('50 Singapore dollars equals this amount
of US dollars:');
console.log(currency.singDollerToUS(50));
console.log('30 US dollars equals this amount of
Singapore dollars:');
console.log(currency.USToSingDoller(30));
18. Reusing modules using node_modules
• Useful for code you’d like to reuse between
applications or share with others
• Allows modules to be required without
knowing their location in the file system
21. The two model of Async Processing
• Callbacks generally define logic for one-off
responses.
– Ex: perform a database query
• Event listeners, are essentially callbacks that
are associated with a conceptual entity (an
event).
– Respond to repeating events
– EX: HTTP server emits a request event when an
HTTP request is made
22. Handling one-off events with
callbacks
• A callback is a function, passed as an
argument to an asynchronous function
• It describes what to do after the asynchronous
operation has completed
var fs = require('fs');
fs.readFile('./resource.json', function (er, data) {
console.log(data);
});
Callback function
23. The Node convention for asynchronous
callbacks
• Most Node built-in modules use callbacks with
two arguments
– First argument is for an error, should one occur.
often abbreviated as er or err
– Second argument is for the results
var fs = require('fs');
fs.readFile('./titles.json', function(er, data) {
if (er) throw er;
// do something with data if no error has occurred
});
24. Handling repeating events with
event emitters
• Event emitters fire events and include the
ability to handle them when triggered
• Events are handled through the use of
listeners
• A listener is the association of an event with a
callback function that gets triggered each time
the event occurs
25. var net = require('net');
var server = net.createServer(function(socket) {
socket.on('data', function(data) {
socket.write(data);
});
});
server.listen(8888);
data events
handled
whenever
new data
has been
read
Using the on method to respond to events
26. Responding to an event that should only
occur once
var net = require('net');
var server = net.createServer(function(socket) {
socket.once ('data', function(data) {
socket.write(data);
});
});
server.listen(8888);
data event
will only
be handled
once
27. References
• [1] Mike Cantelon, Marc Harter, T.J. Holowaychuk and
Nathan Rajlich, Node.js in Action, 1st ed. Manning
Publications, 2014.
• [2] "Node.js - Wikipedia, the free encyclopedia."
[Online]. Available:
http://en.wikipedia.org/wiki/Node.js. [Accessed: 26-
Apr-2015].