-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathserver.js
More file actions
47 lines (39 loc) · 1.11 KB
/
Copy pathserver.js
File metadata and controls
47 lines (39 loc) · 1.11 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
var exec = require('exec');
var express = require('express');
var fs = require('fs');
var path = require('path');
var app = express();
app.use(function(req, res, next){
req.text = '';
req.setEncoding('utf8');
req.on('data', function(chunk){ req.text += chunk });
req.on('end', next);
});
function decodeURL(url) {
return url.replace(/\|/g, '/')
}
// FS stuff.
app.get('/fs/:name', function(req, res) {
res.sendfile(decodeURL(req.params.name));
});
app.post('/fs/:name', function(req, res) {
var filePath = decodeURL(req.params.name);
console.log('Save file: ' + filePath);
fs.writeFileSync(filePath, req.text, 'utf8');
res.status(200).send('saved');
});
// EXEC stuff.
app.post('/exec/', function(req, res) {
var data = JSON.parse(req.text);
exec(data.cmd, data.options || {}, function(err, out, code) {
if (err instanceof Error) {
res.status(500).send(err.toString());
} else {
res.status(200).send(out);
}
});
});
app.use(express.static(__dirname)); // "public" off of current is root
var serverPort = 7777;
app.listen(serverPort);
console.log('Listening on port ' + serverPort);