8. We can do this,
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World¥n');
}).listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');
% node example.js Server running at http://127.0.0.1:8124/
Here comes a web server!
9. Naked Javascript
Pure programming language
ECMAScript
No standard library
Not even document.write()
No package management
unlike PHP, Ruby, Python
Many engines of choice
Rhino, TraceMonkey, Nitro, Chakra, Carakan
20. Titanium
Create native UI with tabs
Titanium.UI.setBackgroundColor('#000');
var tabGroup = Titanium.UI.createTabGroup({id:'tabGroup1'});
var win1 = Titanium.UI.createWindow({id:'win1'});
var tab1 = Titanium.UI.createTab({id:'tab1', window:win1});
tabGroup.addTab(tab1);
tabGroup.open();
Open built-in camera
// Display phone’s camera
Titanium.Media.showCamera({
success: function(event) { ... },
overlay: overlay,
showControls: false,
mediaTypes: Ti.Media.MEDIA_TYPE_PHOTO,
autohide: false
});
21. Node.jsServer-side application
File system, HTTP, SSL, Net, DNS, datagram, process
Open socket and listen to incoming data
var net = require('net');
net.createServer(function (socket) {
socket.write("Echo server¥r¥n");
socket.on("data", function (data) {
socket.write(data);
});
}).listen(8124, "127.0.0.1");
22. CommonJSSpecification of standard API for
non-web application:
CLI, Server, GUI
APIs:
Module
Package
System
Console
Filesystem
HTTP Client
Encodings .. etc.
23. MongoDB
NoSQL database with native Javascript query
SQL: WHERE x = y
> db.foo.find({
$where : function() {
return this.x == this.y;
}
})
OUTPUT: “Hello, Joe”
> db.eval(function(name) {
return "Hello, “ + name;
}, ["Joe"])