This repository was archived by the owner on Jul 24, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequestHandlers.js
More file actions
62 lines (55 loc) · 1.66 KB
/
requestHandlers.js
File metadata and controls
62 lines (55 loc) · 1.66 KB
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
56
57
58
59
60
61
62
var exec = require('child_process').exec;
var path = require('path');
var fs = require('fs');
var mime = require('mime');
// Index Page
exports.index = function (response) {
outputFile(response, path.resolve(__dirname, 'index.html'));
}
// File proxy for assets
exports.asset = function (response, pathname) {
// Pathname starts with a "/". Prefix a dot to make it relative to the current directory
outputFile(response, path.resolve(__dirname, '.'+pathname));
}
// AJAX
exports.ajax = function (response, pathname) {
// Parse path
var pathArray=pathname.split('/');
pathArray.shift(); // Drop "" element
switch(pathArray[1]) {
case 'service':
var service = require ('./service');
service.service(response, pathArray[2], pathArray[3]);
break;
case 'serviceList':
var service = require ('./service');
service.serviceList(response);
break;
default:
response.writeHead(501, {"Content-Type": "text/plain"});
response.write('Not Implemented:'+pathArray[1]);
response.end();
break;
}
}
exports.showDirectory = function (response) {
exec('ls -lah', function(error, stdout, stderr) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write(stdout);
response.end();
});
}
// Helper function to output a file
function outputFile(response, pathname) {
fs.readFile(pathname, null, function(err, data) {
if (err) {
response.writeHead(404, {"Content-Type": "text/plain"});
response.write('404 Not found');
response.end();
return
}
response.writeHead(200, {"Content-Type": mime.lookup(pathname)});
response.write(data);
response.end();
});
}