forked from apex/up-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
43 lines (34 loc) · 907 Bytes
/
app.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
"use strict";
const {spawn} = require('child_process');
const express = require('express');
const app = express();
const {PORT = 3000} = process.env;
app.get('/dir/*', function (req, res) {
spawnPgm('ls', ['-lh', req.path.split('/dir')[1]], (code, output) => {
res.set('Content-Type', 'text/plain');
res.send(output);
});
});
app.get('/cmd/*', function (req, res) {
const pgm = req.path.split('/cmd/')[1];
const args = req.query.args ? req.query.args.split(',') : [];
//
spawnPgm(pgm, args, (code, output) => {
res.set('Content-Type', 'text/plain');
res.send(output);
});
});
function spawnPgm(pgm, args, done) {
const run = spawn(pgm, args);
let output = '';
run.stdout.on('data', (data) => {
output += data;
});
run.stderr.on('data', (data) => {
output += data;
});
run.on('close', (code) => {
done(code, output);
});
}
app.listen(PORT);