app.js
This file
Path
examples/format/app.js
63 lines · 1.5 KiB · JavaScript
Last changed by
Exposing ./support libs to examplesTj Holowaychukbefore adoption9b7470d
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
/** * Module dependencies. */
var express = require('../../lib/express');
var app = express.createServer();
// Fake items
var items = [ { name: 'foo' }, { name: 'bar' }, { name: 'baz' }];
// Routes
app.get('/', function(req, res, next){ res.send('Visit /item/2');});
app.get('/item/:id.:format?', function(req, res, next){ var id = req.params.id, format = req.params.format, item = items[id]; // Ensure item exists if (item) { // Serve the format switch (format) { case 'json': // Detects json res.send(item); break; case 'xml': // Set contentType as xml then sends // the string var xml = '' + '<items>' + '<item>' + item.name + '</item>' + '</items>'; res.contentType('.xml'); res.send(xml); break; case 'html': default: // By default send some hmtl res.send('<h1>' + item.name + '</h1>'); } } else { // We could simply pass route control and potentially 404 // by calling next(), or pass an exception like below. next(new Error('Item ' + id + ' does not exist')); }});
// Middleware
app.use(express.errorHandler({ showStack: true }));
app.listen(3000);console.log('Express app started on port 3000');