-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample.js
47 lines (36 loc) · 996 Bytes
/
sample.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
const Koa = require('koa');
const koaBody = require('koa-body');
const app = module.exports = new Koa();
const { exec } = require('child_process');
let cmd = 'abc'
exec(`echo ${cmd}`, (err, stdout, stderr) => {
if (err) {
// node couldn't execute the command
return;
}
// the *entire* stdout and stderr (buffered)
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
app.use(koaBody({
jsonLimit: '1kb'
}));
// response
// app.use(ctx => {
// ctx.body = 'Hello Koa';
// });
app.use(async function(ctx) {
const body = ctx.request.body;
if (!body.name) ctx.throw(400, '.name required');
exec(`echo ${body.name}`, (err, stdout, stderr) => {
if (err) {
// node couldn't execute the command
return;
}
// the *entire* stdout and stderr (buffered)
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
ctx.body = { name: body.name.toUpperCase() };
});
if (!module.parent) app.listen(54321);