The document discusses the history and capabilities of JavaScript. It describes how JavaScript originated as Livescript for Netscape in 1995 and was not initially meant for manipulating the DOM. It discusses how JavaScript became the main language for developing web applications with the introduction of XMLHttpRequest and how Node.js uses Google's V8 engine and LibUV to provide asynchronous and non-blocking I/O. It promotes Cloud9 as an online IDE for developing JavaScript applications and demonstrates how to build a simple Node.js server and real-time web app with Socket.IO.
13. Java vs. Javascript
Let’s confess:
JavaScript is already the language of the Web
Saturday, February 2, 13
14. People started to care
• VMs got faster
• And embeddable!
• EcmaScript 5
• JSConf
So, JavaScript is a very simple, but often
misunderstood language.
Saturday, February 2, 13
15. Node.js is a NOT another web
framework. I promise !
Saturday, February 2, 13
26. Read File in Ruby
file = File.new("readfile.rb", "r")
while (line = file.gets)
# Do something with line
end
file.close
Read File in Node
fs.readFile('readfile.js', function (err, buffer){
if (err) throw err;
// File is read and we're done.
});
Saturday, February 2, 13
27. browser.js
db.js
server.js
Saturday, February 2, 13
28. Normal C++ IO
• Node.js is fast by design.
• Never blocking on I/O means less
threads.
Saturday, February 2, 13
29. Program
Async.parallel([
function loadData(next) {
db.loadData(next);
},
function readFile(next) {
fs.readFile(fName, next);
], function done(err, items) {
if (err) throw err;
// Do something with items
});
https://github.com/caolan/async
Saturday, February 2, 13
30. Program
var user;
Async.series([
function loadUser(next) {
db.getUser(user_id, function(err, u){
user = u;
next(err);
});
},
function findItems(next) {
var sql = "SELECT * FROM store WHERE
type=?";
db.query(sql, user.type, next);
], function done(err, items) {
if (err) throw err;
// Do something with items
});
Saturday, February 2, 13
31. Program
Mix to do complex stuff like:
https://github.com/caolan/async
Saturday, February 2, 13
32. Program
• Javascript, 1995 to 2013
• Building Cloud9
• Demo
Saturday, February 2, 13
38. You really need HELP
Text
undeclared variable
Saturday, February 2, 13
39. You really need HELP
Text
undeclared variable
Iterating using undeclared variable
Did you mean “length”?
Saturday, February 2, 13
40. You really need HELP
Text
undeclared variable
Iterating using undeclared variable
Did you mean “length”?
Saturday, February 2, 13
41. You really need HELP
Text
undeclared variable
Iterating using undeclared variable
Did you mean “length”?
function created in loop
Warning: you are in an anonymous inner function with its own “this” pointer
Saturday, February 2, 13
48. Create
Deploy
Run/Debug
Share Test
Saturday, February 2, 13
49. Program
• Javascript, 1995 to 2012
• Building Cloud9
• Demo
Saturday, February 2, 13
50. Node.js simple server
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello world');
}).listen(3000);
Saturday, February 2, 13
51. Node.js real web app
express / connect
$ npm install -g express
$ express --sessions --css stylus --ejs jade
$ cd myapp
$ npm install
$ node app.js
app.get('/hello.txt', function(req, res){
res.send('Hello World');
});
app.listen(3000);
console.log('Listening on port 3000');
$ node app
http://expressjs.com/
Saturday, February 2, 13
52. Node.js real web app
Socket.IO 100% JS
Realtime apps made possible blurring the
differences between browser transport mechanisms.
$ npm install socket.io
Server Client
var io = require('socket.io'); <script src="/socket.io/socket.io.js"></script>
// attached to the express app <script>
// or runs standalone on port 80 var socket = io.connect('http://localhost');
io = io.listen(app || 80);
socket.on('message', function (msg) {
io.sockets.on('connection', function (socket) { console.log(msg);
socket.on('message', function (msg) { socket.send({ info: 'trash' });
console.log(msg); });
}); </script>
});
http://socket.io
Saturday, February 2, 13
53. Future (Architect)
• A simple yet powerful plugin system for
large-scale Node.js applications
• Dependency Injection for JavaScript
Managing > ~100K LOC of JS
JS: Dynamically typed - Singly threaded
http://github.com/c9/architect
Saturday, February 2, 13
54. Wrap-Up
• Node.js is brilliant for modern web apps
• If you do realtime app that is meant to be scalable,
you should probably consider Node.js & Socket.io
• Scale your code base: http://github.com/c9/architect
for your application.
• Check out c9.io for a serious online IDE.
Saturday, February 2, 13
55. Text
http://c9.io
Mostafa Eweda
github.com/mostafaeweda
@mostafaeweda
Saturday, February 2, 13