app.js
This file
Path
examples/format/app.js
68 lines · 1.5 KiB · JavaScript
Last changed by
Not known.
History of this fileTake it
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
/** * 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.statusCode = 200; res.setHeader('Content-Type', 'text/html'); res.write('<p>Visit /item/2</p>'); res.write('<p>Visit /item/2.json</p>'); res.write('<p>Visit /item/2.xml</p>'); res.end();});
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');