Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added content-negotiation example

  • Loading branch information...
commit 5d16e6b302541617eb1c8ca07617b457c437e9bd 1 parent 96f7574
TJ Holowaychuk tj authored
Showing with 47 additions and 0 deletions.
  1. +47 −0 examples/content-negotiation/app.js
47 examples/content-negotiation/app.js
View
@@ -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');
Please sign in to comment.
Something went wrong with that request. Please try again.