summaryrefslogtreecommitdiffstats
path: root/express/app2.js
blob: 292741b91c28447e6d3214336afc7ee12eae6cd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
var express = require('express');
var util    = require('util');

var app = express.createServer()
var pagehits = 0;

app.configure('dev', function() {
    app.use(express.errorHandler({
        dumpExceptions: true,
        showStack: true
    }));
});

app.configure(function() {
    app.use(express.logger('dev'));
    app.use(express.favicon());
    app.use(app.router);
    /* empty, next() would lead us here */
});

app.get('/*', function(req, res, next) {
    pagehits++;
    /* look at app.use*, app.router brings us here since we have a GET match here,
     * we need to call next() so we pass control to whoever is next
     */
    next();
});

app.get('/info', function(req, res) {
    res.send('page hits: ' + pagehits + '\n');
});

app.get('/user/:id([0-9]+)', function(req, res) {
    res.send('user ' + req.params.id);
});

app.get('/', function(req, res) {
    res.send('hello, world!\n');
});

app.use(function(req, res) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write('resorting to connect! could do my original routing\n');
    res.end();
});

app.listen(8081);