Increasingly we want to do more with the web and Internet applications we build. We have more features, more data, more users, more devices and all of it needs to be in real-time. With all of these demands how can we keep up? The answer is choosing a language and a platform that are optimized for the kind of architecture Internet and web applications really have. The traditional approach prioritises computation, assigning server resources before they are actually needed. JavaScript and Node.js both take an event driven approach only assigning resources to events as they happen. This allows us to make dramatic gains in performance and resource utilization while still having an environment which is fun and easy to program.
37. Node.js?
• Server Side JavaScript runtime
• Built on top of V8 JavaScript engine from
Google Chrome
• Non-blocking I/O APIs
• Easy to extend APIs and modules
40. > console.log('Hello World');
Hello World
> .help
.clear Break, and also clear the local context.
.exit Exit the prompt
.help Show repl options
> .clear
Clearing context...
> .exit
Enki:~ $
41. Enki:~ $ node
> var foo = "bar";
> foo;
'bar'
> .clear
Clearing context...
> foo
ReferenceError: foo is not defined
at [object Context]:1:1
at Interface.<anonymous> (repl:98:19)
at Interface.emit (events:27:15)
at Interface._ttyWrite (readline:295:12)
at Interface.write (readline:132:30)
at Stream.<anonymous> (repl:79:9)
at Stream.emit (events:27:15)
at IOWatcher.callback (net:489:16)
42. var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Worldn');
}).listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');
43. var http = require('http');
//include the http library
44. http.createServer(function (req, res) {
}).listen(8124, "127.0.0.1");
//create an http server
//when ‘stuff’ happens call this anonymous function
//listen on port 8124 of the IP 127.0.0.1
45. http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Worldn');
})
//when ‘stuff’ happens my function fires
//I get a request object and a response object
//I write to the response object header
//HTTP status 200 and content-type ‘text/plain’
//close the response with the body:
//Hello World
46. console.log('Server running at http://127.0.0.1:8124/');
//write Server is running at http://127.0.0.1:8124/
//to the console
54. var http = require('http');
server = http.createServer();
server.on('request', function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Worldn');
});
server.listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');
55. var http = require('http');
server = http.createServer();
server.on('request', function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Worldn');
});
server.listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');
Step 1.
Evaluate 'Main'
56. var http = require('http');
server = http.createServer();
server.on('request', function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Worldn');
});
server.listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');
Step 1.
variables: http -> http module
server -> http server
listeners: server.request -> function
57. var http = require('http');
server = http.createServer();
*
server.on('request', function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Worldn');
});
server.listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');
Step 2.
Event Loop
58. var http = require('http');
server = http.createServer();
*
server.on('request', function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Worldn');
});
server.listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');
Step 2.
Do we have active listeners?
listeners: server.request -> function
Yes! Wait for listeners.
59. var http = require('http');
server = http.createServer();
server.on('request', function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Worldn');
});
server.listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');
Step 3.
Event Calls
60. var http = require('http');
server = http.createServer();
server.on('request', function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Worldn');
});
server.listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');
Step 3.
'request' is called. Since
listeners: server.request -> function
Call function
61. var http = require('http');
server = http.createServer();
server.on('request', function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Worldn');
});
server.listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');
Step 3.
Loop!
(go to Step 2.)