13
SenchaLabs Connect & Express High Performance Servers written in JavaScript Monday, September 20, 2010

SenchaLabs Connect & Express

Embed Size (px)

DESCRIPTION

This is a talk I gave at a local ExtJS users group at Sencha headquarters.

Citation preview

Page 1: SenchaLabs Connect & Express

SenchaLabs Connect & ExpressHigh Performance Servers written in JavaScript

Monday, September 20, 2010

Page 2: SenchaLabs Connect & Express

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

Page 3: SenchaLabs Connect & Express

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

Page 4: SenchaLabs Connect & Express

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

Page 5: SenchaLabs Connect & Express

Hello World in Node.js// Load the http modulevar http = require('http');

// Setup a request handler for a simple HTTP server.// Listen on port 3000http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n');}).listen(3000, "127.0.0.1");

// Print a pretty message to the terminalconsole.log('Server running at http://127.0.0.1:3000/');

Monday, September 20, 2010

Page 6: SenchaLabs Connect & Express

Hello World in Connect// Load the connect moduleConnect = require('connect');// Setup a simple Connect serverserver = Connect.createServer( function (req, res) { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Hello World\n'); });// Export the server so Spark can run itmodule.exports = server;

Monday, September 20, 2010

Page 7: SenchaLabs Connect & Express

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

Page 8: SenchaLabs Connect & Express

Bundled ModulesStatic File Server

Apache Style Logs

Body Decoder

Cookie Decoder

Session Provider

Request Router

Fancy Error Handler

Cache Manifest

Conditional Get

Inline Gzipping

Virtual Host Router

Sass/Less Compiler

Monday, September 20, 2010

Page 9: SenchaLabs Connect & Express

Connect with LayersConnect.createServer( Connect.logger(), Connect.conditionalGet(), Connect.cache(), Connect.gzip(), Connect.staticProvider("public"), Connect.bodyDecoder(), Connect.cookieDecoder(), Connect.session(), Connect.router(routes), Connect.errorHandler({showStack: true}));

Monday, September 20, 2010

Page 10: SenchaLabs Connect & Express

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

Page 11: SenchaLabs Connect & Express

Express Example// Create an express appvar app = express.createServer();

// Setup a route handlerapp.get('/', function(req, res){ res.send('Hello World');});

// Start the HTTP serverapp.listen(3000);

Monday, September 20, 2010

Page 12: SenchaLabs Connect & Express

DEMO TIME!

Monday, September 20, 2010

Page 13: SenchaLabs Connect & Express

http://senchalabs.github.com/connect

http://expressjs.com

http://nodejs.org

http://raphaeljs.com

Links to Projects Used

Monday, September 20, 2010