Node.js and JavaScript are well-suited for Internet applications because Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, capable of supporting many more concurrent connections than traditional server-side models like Apache. This event loop system allows Node.js to handle multiple requests simultaneously without blocking any specific request. It also minimizes memory usage so more requests can be served from fewer servers.
43. 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
46. > 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:~ $
47. 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)
48. 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/');
49. var http = require('http');
//include the http library
50. 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
51. 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
52. 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
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/');
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 1.
Evaluate 'Main'
62. 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
63. 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
64. 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.
65. 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
66. 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
67. 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.)