-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequestHandlers.js
74 lines (66 loc) · 1.99 KB
/
requestHandlers.js
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
63
64
65
66
67
68
69
70
71
72
73
74
var exec = require("child_process").exec;
var querystring = require("querystring");
var fs = require('fs');
var formidable = require('formidable');
function start(res){
console.log("reqHandler - start");
//to make longer calls non-blocking
exec("ls -lah", function(errro, stdout, stderr){
res.writeHead(200, {"Content-Type": "text/plain"});
res.write(stdout);
res.end();
});
}
function upload(res){
console.log("reqHandler - upload");
var body = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" content="text/html; '+
'charset=UTF-8" />'+
'</head>'+
'<body>'+
'<form action="/receive" enctype="multipart/form-data" method="post">'+
'<input type="file" name="upload">'+
'<input type="submit" value="Upload file" />'+
'</form>'+
'</body>'+
'</html>';
// res.writeHead(200, {"Content-Type": "text/plain"});
res.write(body);
res.end();
}
function receive(res, req){
var form = new formidable.IncomingForm();
console.log("about to parse");
form.parse(req, function(error, fields, files){
if(error){
res.writeHead(500, {"Content-Type": "text/plain"});
res.write("error in upload:\n " + error + "\n");
res.end();
}else{
res.writeHead(200, {"Content-Type": "text/html"});
res.write("received image: <br /> <img src='/show?path="+files.upload.path+"' />");
res.end();
}
});
}
function show(res, req){
console.log(querystring.parse(req.url)['/show?path']);
fs.readFile(querystring.parse(req.url['/show?path'])['path'], "binary", function(error, file) {
if(error) {
response.writeHead(500, {"Content-Type": "text/plain"});
console.log('error: '+error);
response.write(error + "\n");
response.end();
} else {
console.log('file success');
response.writeHead(200, {"Content-Type": "image/png"});
response.write(file, "binary");
response.end();
}
});
}
exports.start = start;
exports.upload = upload;
exports.receive = receive;
exports.show = show;