index.js
This file
Path
examples/content-negotiation/index.js
44 lines · 989 bytes · JavaScript
Last changed by
Not known.
History of this fileTake it
The walk stopped after 200 commits: lines older than 6bc7574 point at it.
docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 1–44why, one step back1var express = require('../../');2var app = module.exports = express();3var users = require('./db');45// so either you can deal with different types of formatting6// for expected response in index.js7app.get('/', function(req, res){8 res.format({9 html: function(){10 res.send('<ul>' + users.map(function(user){11 return '<li>' + user.name + '</li>';12 }).join('') + '</ul>');13 },1415 text: function(){16 res.send(users.map(function(user){17 return ' - ' + user.name + '\n';18 }).join(''));19 },2021 json: function(){22 res.json(users);23 }24 });25});2627// or you could write a tiny middleware like28// this to add a layer of abstraction29// and make things a bit more declarative:3031function format(path) {32 var obj = require(path);33 return function(req, res){34 res.format(obj);35 };36}3738app.get('/users', format('./users'));3940/* istanbul ignore next */41if (!module.parent) {42 app.listen(3000);43 console.log('Express started on port 3000');44}