Skip to content

Commit

Permalink
added content-negotiation example
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Jun 6, 2011
1 parent 96f7574 commit 5d16e6b
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions examples/content-negotiation/app.js
@@ -0,0 +1,47 @@

/**
* Module dependencies.
*/

var express = require('../../lib/express');

var app = express.createServer();

var users = [
{ name: 'tobi' }
, { name: 'loki' }
, { name: 'jane' }
];

function provides(type) {
return function(req, res, next){
if (req.accepts(type)) return next();
next('route');
}
}

// curl http://localhost:3000/users -H "Accept: application/json"

app.get('/users', provides('json'), function(req, res){
res.send(users);
});

// curl http://localhost:3000/users -H "Accept: text/html"

app.get('/users', provides('html'), function(req, res){
res.send('<ul>' + users.map(function(user){
return '<li>' + user.name + '</li>';
}).join('\n') + '</ul>');
});

// curl http://localhost:3000/users -H "Accept: text/plain"

app.get('/users', function(req, res, next){
res.contentType('txt');
res.send(users.map(function(user){
return user.name;
}).join(', '));
});

app.listen(3000);
console.log('Express server listening on port 3000');

0 comments on commit 5d16e6b

Please sign in to comment.