updated content-negotiation example
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by TJ Holowaychuk · committed by TJ Holowaychuk · parent bdfa6d1 · Browse files · 2 files·+33 added−81 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+33 added−81 removed
- tests:+9 added−37 removed
- other:+24 added−44 removed
Provenance
- Commit
a21435c- Tree
64667c2- Author
TJ Holowaychuk <tj@vision-media.ca>- Committer
TJ Holowaychuk <tj@vision-media.ca>- Parent
bdfa6d1
2 files changed+33 added−81 removed
modified:examples/content-negotiation/index.js+24 added-44 removed
123423465689789101112131415111213141516171817181920212223242519202122232427282930252627282930333435363738394041424344454647484950313253543334
/** * Module dependencies. */var express = require('../../') , app = module.exports = express();
var express = require('../../');var users = [];
// expose the app for require()ingvar app = module.exports = express();users.push({ name: 'Tobi' });users.push({ name: 'Loki' });users.push({ name: 'Jane' });
var users = [ { name: 'tobi' } , { name: 'loki' } , { name: 'jane' }];app.get('/', function(req, res){ res.respondTo({ 'text/html': function(){ res.send('<ul>' + users.map(function(user){ return '<li>' + user.name + '</li>'; }).join('') + '</ul>'); },
function provides(type) { return function(req, res, next){ if (req.accepts(type)) return next(); // invoking next() with "route" will // skip passed all remaining middleware // for this route (if any). next('route'); }} 'text/plain': function(){ res.send(users.map(function(user){ return ' - ' + user.name + '\n'; }).join('')); },
// curl http://localhost:3000/users -H "Accept: application/json"
app.get('/users', provides('json'), function(req, res){ res.send(users); 'application/json': function(){ res.json(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.type('txt'); res.send(users.map(function(user){ return user.name; }).join(', '));});
// not being require()dif (!module.parent) { app.listen(3000); console.log('Express server listening on port 3000');} console.log('listening on port 3000');}No newline at end of filemodified:test/acceptance/content-negotiation.js+9 added-37 removed
123456767891091012131411121613141519161721222324252627282930313233181920363738212240232442434445464748495051522526
var request = require('../support/http') , app = require('../../examples/content-negotiation');
describe('content-negotiation', function(){ describe('GET /users', function(){ it('should support json', function(done){ describe('GET /', function(){ it('should default to text/html', function(done){ request(app) .get('/users') .set('Accept', 'application/json') .get('/') .end(function(res){ res.should.have.header('Content-Type', 'application/json; charset=utf-8'); res.should.have.status(200); res.body.should.equal('[{"name":"tobi"},{"name":"loki"},{"name":"jane"}]'); res.body.should.equal('<ul><li>Tobi</li><li>Loki</li><li>Jane</li></ul>'); done(); }); }) })
it('should support html', function(done){ it('should accept to text/plain', function(done){ request(app) .get('/users') .set('Accept', 'text/html') .end(function(res){ res.should.have.header('Content-Type', 'text/html; charset=utf-8'); res.should.have.status(200); res.body.should.equal('<ul><li>tobi</li>\n<li>loki</li>\n<li>jane</li></ul>'); done(); }); })
it('should support plain text', function(done){ request(app) .get('/users') .get('/') .set('Accept', 'text/plain') .end(function(res){ res.should.have.header('Content-Type', 'text/plain'); res.should.have.status(200); res.body.should.equal('tobi, loki, jane'); res.body.should.equal(' - Tobi\n - Loki\n - Jane\n'); done(); }); }) })
it('should default to application/json', function(done){ request(app) .get('/users') .end(function(res){ res.should.have.header('Content-Type', 'application/json; charset=utf-8'); res.should.have.status(200); res.body.should.equal('[{"name":"tobi"},{"name":"loki"},{"name":"jane"}]'); done(); }); }) })})No newline at end of fileNo newline at end of file