Permalink
Browse files

added content-negotiation example

  • Loading branch information...
1 parent 96f7574 commit 5d16e6b302541617eb1c8ca07617b457c437e9bd @tj tj committed Jun 6, 2011
Showing with 47 additions and 0 deletions.
  1. +47 −0 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.