summaryrefslogtreecommitdiffstats
path: root/route.js
blob: 0b25fa4070f2923db47e0d4abb1d6c0a155af27e (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
/*
 * route.js
 *
 * used for handling pathnames in url query
 */

var util = require('util');

var pnhandler = require('./pnhandler');

/* list of handlers, not a global variable */
var handle = {
    '/': pnhandler.noop,
    '/register': pnhandler.register,
    '/login': pnhandler.login,
    '/poll': pnhandler.poll,
    '/upload': pnhandler.upload,
    '/info' : pnhandler.info,
    '/hello': pnhandler.hello,
    '/hellores': pnhandler.hellores
};

function route(query, reqdata, res) {
    if (typeof handle[query.pathname] === 'function' ) {
        handle[query.pathname](query, reqdata, res);
    } else {
        util.log('[route] no handler found for ' + query.pathname);

        res.writeHead(404, {'Content-Type': 'text/html'});
        res.write('<!DOCTYPE html>\n' +
                    '<html>\n' +
                    '<body>\n' +
                    '\t404 Not found\n' +
                    '</body>\n' +
                  '</html>\n');
        res.end();
    }
}

exports.route = route;