summaryrefslogtreecommitdiffstats
path: root/express/app2.js
blob: db95537a164add7d90eb38ce246c7d26945a7ca1 (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
49
50
51
52
53
54
55
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(express.methodOverride());
    /* parse request bodies, place the result in req.body */
    app.use(express.bodyParser());
    app.use(app.router);
    var oneYear = 31557600000;
    app.use(express.static(__dirname + '/public', { maxAge: oneYear }));

    app.set('views', __dirname + '/views');
});

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);