1 var url
= require('url'),
2 node_static
= require ('node-static');
6 var HttpHandler = function (config
) {
7 var public_html
= config
.public_html
|| 'client/';
8 this.file_server
= new node_static
.Server(public_html
);
11 module
.exports
.HttpHandler
= HttpHandler
;
15 HttpHandler
.prototype.serve = function (request
, response
) {
16 // The incoming requests base path (ie. /kiwiclient)
17 var base_path
= global
.config
.http_base_path
|| '/kiwi',
20 // Trim of any trailing slashes
21 if (base_path
.substr(base_path
.length
- 1) === '/') {
22 base_path
= base_path
.substr(0, base_path
.length
- 1);
25 // Build the regex to match the base_path
26 base_path_regex
= base_path
.replace(/[-/\\^$*+?.()|[\]{}]/g
, '\\$&');
28 // Any asset request to head into the asset dir
29 request
.url
= request
.url
.replace(base_path
+ '/assets/', '/assets/');
31 // Any requests for /client to load the index file
32 if (request
.url
.match(new RegExp('^' + base_path_regex
+ '([/$]|$)', 'i'))) {
37 this.file_server
.serve(request
, response
, function (err
) {
39 response
.writeHead(err
.status
, err
.headers
);