httphandler.js rename. client.sendIRCCommand naming
[KiwiIRC.git] / server / httphandler.js
1 var url = require('url'),
2 node_static = require ('node-static');
3
4
5
6 var HttpHandler = function (config) {
7 var public_html = config.public_html || 'client/';
8 this.file_server = new node_static.Server(public_html);
9 };
10
11 module.exports.HttpHandler = HttpHandler;
12
13
14
15 HttpHandler.prototype.serve = function (request, response) {
16 // The incoming requests root directory (ie. /kiwiclient/)
17 // TODO: check the config for this setting
18 var root_path = '/client',
19 root_path_regex = root_path.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
20
21 // Any asset request to head into the asset dir
22 request.url = request.url.replace(root_path + '/assets/', '/assets/');
23
24 // Any requests for /client to load the index file
25 if (request.url.match(new RegExp('^' + root_path_regex, 'i'))) {
26 request.url = '/';
27 }
28
29
30 this.file_server.serve(request, response, function (err) {
31 if (err) {
32 response.writeHead(err.status, err.headers);
33 response.end();
34 }
35 });
36 };