index.js
This file
Path
examples/content-negotiation/index.js
46 lines · 1003 bytes · JavaScript
Last changed by
Not known.
History of this fileTake it
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647'use strict'
var express = require('../../');var app = module.exports = express();var users = require('./db');
// so either you can deal with different types of formatting// for expected response in index.jsapp.get('/', function(req, res){ res.format({ html: function(){ res.send('<ul>' + users.map(function(user){ return '<li>' + user.name + '</li>'; }).join('') + '</ul>'); },
text: function(){ res.send(users.map(function(user){ return ' - ' + user.name + '\n'; }).join('')); },
json: function(){ res.json(users); } });});
// or you could write a tiny middleware like// this to add a layer of abstraction// and make things a bit more declarative:
function format(path) { var obj = require(path); return function(req, res){ res.format(obj); };}
app.get('/users', format('./users'));
/* istanbul ignore next */if (!module.parent) { app.listen(3000); console.log('Express started on port 3000');}