summaryrefslogtreecommitdiffstats
path: root/express/app3.js
blob: 0c2acd426860ac09fafa70efcc147f52949207b5 (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
/*
 * diff: does simple error handling
 * reqmnts: $ pacman -S redis and start daemon
 */

var express   = require('express');
var util      = require('util');
var RedisStore = require('connect-redis')(express);

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(express.cookieParser());
    app.use(express.session({ secret: "keyboard cat", store: new RedisStore }));
    /* should be on the bottom of this block? */
    app.use(app.router);
    app.use(express.static(__dirname + '/public', { maxAge: 31557600000 /* one year */ }));
    app.set('views', __dirname + '/views');
});

app.get('/*', function(req, res, next) {
    pagehits++;
    next();
});
app.get('/info', function(req, res) {
    res.send('page hits: ' + pagehits + '\n');
});
app.get('/', function(req, res) {
    res.send('hello, world!\n');
});
app.get('/user/:id([0-9]+)', function(req, res) {
    res.send('user ' + req.params.id);
});
app.get('/*', function(req, res) {
    throw new Error('the fuck you doing?!');
});
app.error(function(err, req, res, next) {
    next(err); /* needs to be here or page load will stall on wrong pn */
});

app.listen(8081);