Skip to content
Myr

app.js

This file

Path

examples/format/app.js

69 lines · 1.4 KiB · JavaScript

Last changed by

Merge branch 'integration'TJ Holowaychukbefore adoption0ae024a

History of this file

Take it

The walk stopped after 200 commits: lines older than 7e352fe point at it.

Added app.render() app.locals testTj Holowaychukbefore adoption7e352fe

lines 1–5why, one step back
1
2/**3 * Module dependencies.4 */5

Merge branch 'integration'TJ Holowaychukbefore adoption0ae024a

line 6why, one step back
6var express = require('../../');

Added app.render() app.locals testTj Holowaychukbefore adoption7e352fe

line 7why, one step back
7

Merge branch 'integration'TJ Holowaychukbefore adoption0ae024a

line 8why, one step back
8var app = module.exports = express();

Added app.render() app.locals testTj Holowaychukbefore adoption7e352fe

lines 9–47why, one step back
9
10// Fake items1112var items = [13 { name: 'foo' }14 , { name: 'bar' }15 , { name: 'baz' }16];1718// Routes1920app.get('/', function(req, res, next){21 res.statusCode = 200;22 res.setHeader('Content-Type', 'text/html');23 res.write('<p>Visit /item/2</p>');24 res.write('<p>Visit /item/2.json</p>');25 res.write('<p>Visit /item/2.xml</p>');26 res.end();27});2829app.get('/item/:id.:format?', function(req, res, next){30 var id = req.params.id31 , format = req.params.format32 , item = items[id];33 // Ensure item exists34 if (item) {35 // Serve the format36 switch (format) {37 case 'json':38 // Detects json39 res.send(item);40 break;41 case 'xml':42 // Set contentType as xml then sends43 // the string44 var xml = ''45 + '<items>'46 + '<item>' + item.name + '</item>'47 + '</items>';

Merge branch 'integration'TJ Holowaychukbefore adoption0ae024a

line 48why, one step back
48 res.type('xml').send(xml);

Added app.render() app.locals testTj Holowaychukbefore adoption7e352fe

lines 49–63why, one step back
49 break;
50 case 'html':51 default:52 // By default send some hmtl53 res.send('<h1>' + item.name + '</h1>');54 }55 } else {56 // We could simply pass route control and potentially 40457 // by calling next(), or pass an exception like below.58 next(new Error('Item ' + id + ' does not exist'));59 }60});6162// Middleware63

Merge branch 'integration'TJ Holowaychukbefore adoption0ae024a

line 64why, one step back
64app.use(express.errorHandler());

Added app.render() app.locals testTj Holowaychukbefore adoption7e352fe

line 65why, one step back
65

Merge branch 'integration'TJ Holowaychukbefore adoption0ae024a

lines 66–69why, one step back
66if (!module.parent) {
67 app.listen(3000);68 silent || console.log('Express started on port 3000');69}