1. SenchaLabs Connect & Express
High Performance Servers written in JavaScript
Monday, September 20, 2010
2. Why we need non-blocking
Polling is too slow and inefficient.
Live interaction requires the server to push data.
In order to push data, the connections need to be
persistent.
The server needs to handle thousands of
persistent connections.
Threads aren’t going to scale.
Monday, September 20, 2010
3. Why we need JavaScript
You already use it in the client.
It’s already event based and not threaded.
It’s actually a good language if used right.
Lambdas, closures, garbage collection.
V8 is a really fast VM
Monday, September 20, 2010
4. Node's goal is to provide an easy way to build
performant network programs.
This is in contrast to today's more common
concurrency model where OS threads are
employed.
Node programs are written in JavaScript.
Monday, September 20, 2010
5. Hello World in Node.js
// Load the http module
var http = require('http');
// Setup a request handler for a simple HTTP server.
// Listen on port 3000
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/
plain'});
res.end('Hello Worldn');
}).listen(3000, "127.0.0.1");
// Print a pretty message to the terminal
console.log('Server running at http://
127.0.0.1:3000/');
Monday, September 20, 2010
6. Hello World in Connect
// Load the connect module
Connect = require('connect');
// Setup a simple Connect server
server = Connect.createServer(
function (req, res) {
res.writeHead(200, {
'Content-Type': 'text/plain'
});
res.end('Hello Worldn');
}
);
// Export the server so Spark can run it
module.exports = server;
Monday, September 20, 2010
7. Stacking in Middleware
Node provides very low level APIs
There is no cookie handling or parsing.
There is no session support built-in.
There is no routing built-in.
There is no static file serving.
Connect makes this easy!
Monday, September 20, 2010
8. Bundled Modules
Static File Server Fancy Error Handler
Apache Style Logs Cache Manifest
Body Decoder Conditional Get
Cookie Decoder Inline Gzipping
Session Provider Virtual Host Router
Request Router Sass/Less Compiler
Monday, September 20, 2010
10. Express is even easier
Connect is a framework maker’s framework.
It’s much easier than raw node.js, but still low
level.
Express is a simple framework for application
developers.
It’s inspired by Sinatra from the Ruby world.
Monday, September 20, 2010
11. Express Example
// Create an express app
var app = express.createServer();
// Setup a route handler
app.get('/', function(req, res){
res.send('Hello World');
});
// Start the HTTP server
app.listen(3000);
Monday, September 20, 2010