Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
express
high class web development for nodeJS
Dennis Blöte, Rails Usergroup Hamburg, 11/10/2010
What‘s express?
• nodeJS based web framework
• inspired by Sinatra
• asynchronous
var app = express.createServer();
app.ge...
• Install nodeJS
• Install npm (node package manager)
• Install express
Installation
$ npm install express
$ brew install ...
Configuration (1)
app.configure(function(){
app.use(app.router);
app.use(express.staticProvider(__dirname + '/public'));
}...
Configuration (2)
$ NODE_ENV=production node app.js
Starting with a specific environment:
app.configure(function(){
app.se...
Routing
app.get('/users/:id?', function(req, res) {
var id = req.params.id;
res.send(id ? 'user ' + id : 'users');
});
app...
Connect / Middlewares
• Connect is a middleware framework
• Similar to Rack
• A middleware is simply a function with
three...
Middlewares
var loadUser = function(req, res, next) {
var id = req.params.id;
var user = db.loadUser(id); // fetch user fr...
HTTP POST
<form method="post" action="/">
<input type="text" name="user[name]" />
<input type="submit" value="Submit" />
<...
HTTP PUT/DELETE
<form method="post" action="/">
<input type="hidden" name="_method" value="put" />
<input type="text" name...
View Rendering
• Concept: Layout, view and partials
• Template Engines: Jade, Haml, EJS, …
• View variables are passed to ...
Jade - Template Engine
!!! 5
html(lang="en")
head
title= pageTitle
:javascript
| if (foo)
| bar()
body
h1 Jade - node temp...
Helpers
app.dynamicHelpers({
flashes: function(req, res) {
var msg = req.flash('message');
return msg.length ? '<p id="msg...
Sessions
• Currently depending on cookies
• Store implementations: Memory, Redis, …
app.use(express.cookieDecoder());
app....
Error Handling
app.use(express.errorHandler({ dumpExceptions: true }));
app.error(function(err, req, res, next) {
if (err ...
Why express?
• Full-blown feature set
• Built on Connect
• Good documentation
• Lots of examples
• Many extensions
• Nice ...
Going further…
• http://expressjs.com/guide.html
• http://expressjs.com/api.html
• http://senchalabs.github.com/connect/
•...
Upcoming SlideShare
Loading in …5
×

ExpressJS

13,993 views

Published on

ExpressJS is a web development framework for nodeJS. It is inspired by Ruby's Sinatra and brings you a full-blown list of features to develop an asynchronous web application.

Published in: Technology, Design

ExpressJS

  1. 1. express high class web development for nodeJS Dennis Blöte, Rails Usergroup Hamburg, 11/10/2010
  2. 2. What‘s express? • nodeJS based web framework • inspired by Sinatra • asynchronous var app = express.createServer(); app.get('/', function(req, res){ res.send('Hello World'); }); app.listen(3000);
  3. 3. • Install nodeJS • Install npm (node package manager) • Install express Installation $ npm install express $ brew install npm $ brew install node
  4. 4. Configuration (1) app.configure(function(){ app.use(app.router); app.use(express.staticProvider(__dirname + '/public')); }); app.configure('development', function(){ app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); }); app.configure('production', function(){ app.use(express.errorHandler()); });
  5. 5. Configuration (2) $ NODE_ENV=production node app.js Starting with a specific environment: app.configure(function(){ app.set('views', __dirname + '/views'); app.set('views'); // => returns views dir app.enable('feature'); // sets feature to true app.disable('feature'); // sets feature to false });
  6. 6. Routing app.get('/users/:id?', function(req, res) { var id = req.params.id; res.send(id ? 'user ' + id : 'users'); }); app.post('/users/:id.:format', function(req, res) { var id = req.params.id; var format = req.params.format; res.send('user: ' + id + ' format: ' + format); }); app.get('/file/*.*', function(req, res) { res.send('path: ' + req.params); });
  7. 7. Connect / Middlewares • Connect is a middleware framework • Similar to Rack • A middleware is simply a function with three arguments: request, response, next var helloWorld = function(req, res, next) { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('hello world'); }
  8. 8. Middlewares var loadUser = function(req, res, next) { var id = req.params.id; var user = db.loadUser(id); // fetch user from db if (user) { req.user = user; next(); } else { next(new Error('Failed to load user ' + id)); } } app.get('/user/:id', loadUser, function(req, res) { res.send('Viewing user ' + req.user.name); });
  9. 9. HTTP POST <form method="post" action="/"> <input type="text" name="user[name]" /> <input type="submit" value="Submit" /> </form> bodyDecoder middleware for POST params: app.use(express.bodyDecoder()); app.post('/', function(req, res){ console.log(req.body.user); res.redirect('back'); });
  10. 10. HTTP PUT/DELETE <form method="post" action="/"> <input type="hidden" name="_method" value="put" /> <input type="text" name="user[name]" /> <input type="submit" value="Submit" /> </form> bodyDecoder and methodOverride: app.use(express.bodyDecoder()); app.use(express.methodOverride()); app.post('/', function(req, res){ console.log(req.body.user); res.redirect('back'); });
  11. 11. View Rendering • Concept: Layout, view and partials • Template Engines: Jade, Haml, EJS, … • View variables are passed to render app.set('view engine', 'jade'); app.get('/', function(req, res){ res.render('index.haml', { layout: 'app', // -> app.jade locals: { title: 'This is my app' } }); });
  12. 12. Jade - Template Engine !!! 5 html(lang="en") head title= pageTitle :javascript | if (foo) | bar() body h1 Jade - node template engine #container - if (youAreUsingJade) p You are amazing - else p Get on it! .comments = partial('comment', comments)
  13. 13. Helpers app.dynamicHelpers({ flashes: function(req, res) { var msg = req.flash('message'); return msg.length ? '<p id="msg">' + msg + '</p>': ''; } }); app.helpers({ linkTo: function(text, url) { return '<a href=' + url + '>' + text + '</a>'; }, name: function(user) { return user.firstName + ' ' + user.lastName; } });
  14. 14. Sessions • Currently depending on cookies • Store implementations: Memory, Redis, … app.use(express.cookieDecoder()); app.use(express.session()); app.post('/cart/add', function(req, res) { req.session.items = req.body.items; res.redirect('back'); }); app.get('/cart', function(req, res) { var items = req.session.items; res.render('cart', { locals: { items: items } }); });
  15. 15. Error Handling app.use(express.errorHandler({ dumpExceptions: true })); app.error(function(err, req, res, next) { if (err instanceof Unauthorized) { res.render('401.jade', { locals: { error: err } }); } else if (err instanceof NotFound) { res.render('404.jade'); } else { next(err); } }); The app.error() method receives exceptions thrown within a route, or passed to next(err)
  16. 16. Why express? • Full-blown feature set • Built on Connect • Good documentation • Lots of examples • Many extensions • Nice community
  17. 17. Going further… • http://expressjs.com/guide.html • http://expressjs.com/api.html • http://senchalabs.github.com/connect/ • http://github.com/senchalabs/connect/wiki • http://github.com/visionmedia/express/tree/ master/examples/ • https://github.com/dbloete/Codeshelver

×