written from this video
ryan dahl video
client DOM – top is window
server DOM – top is “process”
ports >8000 require no special privileges
process.pid – shows the process id of node process
process.env – shows the environmental variables of the node process
node is built on v8, the chrome js engine
var http=require(‘http’);
var s = http.createServer(function(req, res) {
res.writeHead(200, {‘content-type’ :‘text/plain’ });
res.write(‘test’);
res.end (“hello world\n”);
});
s.listen (8000);
curl -i http://localhost:8000/
marks hello world program test
HTTP/1.1 200 OK
content-type: text/plain
Date: Mon, 07 Apr 2014 05:27:43 GMT
Connection: keep-alive
Transfer-Encoding: chunked
connection: keep-alive – new in http 1.1 “persistent connection”
ab – apache bench tool
ab -n 100 -c 100 http://127.0.0.1 ; -n requests Number of requests to perform -c concurrency Number of multiple requests to make
listen command – binds server to port!
createServer and Server are the same (shorter)
===
var net = require(‘net’);
//var server = net.createServer(function(socket) {
var server = net.Server(function(socket) {
socket.write(“hello\n”);
socket.write(“world\n”);
// socket.end shuts down connection
// socket.end(“world\n”);
socket.on(‘data’, function(data) {
socket.write(data);
});
});
server.listen(8000);
crude chat server:
net = require(‘net’);
var sockets = [];
var s = net.Server(function(socket) {
console.log(‘after net.Server command’);
sockets.push(socket);
socket.on(‘data’, function(d) {
for (var i=0; i<sockets.length;i++) {
if ( sockets[i] == socket) continue;
sockets[i].write(d);
}
});
socket.on(‘end’, function() {
var i = sockets.indexOf(socket);
sockets.splice(i, 1);
});
});
s.listen(8000);
== debugging
node debugger – gdb debugger
debugger; — command put into jscript
node debug scriptname.js
node inspector debugger