Commit | Line | Data |
---|---|---|
bd299b17 | 1 | var url = require('url'), |
795014b1 | 2 | node_static = require ('node-static'); |
a8bf3ea4 | 3 | |
a8bf3ea4 | 4 | |
a8bf3ea4 | 5 | |
186531ed D |
6 | var HttpHandler = function (config) { |
7 | var public_html = config.public_html || 'client/'; | |
8 | this.file_server = new node_static.Server(public_html); | |
a8bf3ea4 JA |
9 | }; |
10 | ||
186531ed D |
11 | module.exports.HttpHandler = HttpHandler; |
12 | ||
13 | ||
14 | ||
15 | HttpHandler.prototype.serve = function (request, response) { | |
b65ad8f1 | 16 | // The incoming requests base path (ie. /kiwiclient) |
b737610b | 17 | var base_path = global.config.http_base_path || '/kiwi', |
b65ad8f1 D |
18 | base_path_regex; |
19 | ||
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); | |
23 | } | |
24 | ||
25 | // Build the regex to match the base_path | |
26 | base_path_regex = base_path.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&'); | |
0ae87edb D |
27 | |
28 | // Any asset request to head into the asset dir | |
b65ad8f1 | 29 | request.url = request.url.replace(base_path + '/assets/', '/assets/'); |
0ae87edb | 30 | |
b075a0d6 | 31 | // Any requests for /client to load the index file |
b65ad8f1 | 32 | if (request.url.match(new RegExp('^' + base_path_regex + '([/$]|$)', 'i'))) { |
b075a0d6 D |
33 | request.url = '/'; |
34 | } | |
35 | ||
0ae87edb | 36 | |
186531ed | 37 | this.file_server.serve(request, response, function (err) { |
a8bf3ea4 | 38 | if (err) { |
bd299b17 JA |
39 | response.writeHead(err.status, err.headers); |
40 | response.end(); | |
a8bf3ea4 JA |
41 | } |
42 | }); | |
bd299b17 | 43 | }; |