Permalink
Browse files

Added another example to content-negotiation

  • Loading branch information...
1 parent 9b09257 commit f25aaf11e93b091a0ba2d21c860592fd6e9d0388 @tj tj committed Jun 14, 2012
View
8 examples/content-negotiation/db.js
@@ -0,0 +1,8 @@
+
+var users = [];
+
+users.push({ name: 'Tobi' });
+users.push({ name: 'Loki' });
+users.push({ name: 'Jane' });
+
+module.exports = users;
View
21 examples/content-negotiation/index.js
@@ -1,12 +1,7 @@
var express = require('../../')
- , app = module.exports = express();
-
-var users = [];
-
-users.push({ name: 'Tobi' });
-users.push({ name: 'Loki' });
-users.push({ name: 'Jane' });
+ , app = module.exports = express()
+ , users = require('./db');
app.get('/', function(req, res){
res.format({
@@ -28,6 +23,18 @@ app.get('/', function(req, res){
})
});
+// or you could write a tiny middleware like
+// this to abstract make things a bit more declarative:
+
+function format(mod) {
+ var obj = require(mod);
+ return function(req, res){
+ res.format(obj);
+ }
+}
+
+app.get('/users', format('./users'));
+
if (!module.parent) {
app.listen(3000);
console.log('listening on port 3000');
View
18 examples/content-negotiation/users.js
@@ -0,0 +1,18 @@
+
+var users = require('./db');
+
+exports.html = function(req, res){
+ res.send('<ul>' + users.map(function(user){
+ return '<li>' + user.name + '</li>';
+ }).join('') + '</ul>');
+};
+
+exports.text = function(req, res){
+ res.send(users.map(function(user){
+ return ' - ' + user.name + '\n';
+ }).join(''));
+};
+
+exports.json = function(req, res){
+ res.json(users);
+};

0 comments on commit f25aaf1

Please sign in to comment.