简体   繁体   中英

send stdout/stderr using node and express

I have this code

const express = require('express')
const app = express()

app.get('/', function (req, res) {
    var process = require('child_process');
    process.exec('hadoop fs -ls',function (err,stdout,stderr) {
        if (err) {
            console.log(typeof(stderr));
            console.log("\n"+stderr);
            res.send(stderr);
        } else {
            console.log(stdout);
        }
    });
    res.send('Hello World!')
})

app.listen(3000, function () {
    console.log('Example app listening on port 3000!')
})

I hope it can send back the cmd output to the client, but I get this error:

C:\Users\Administrator\Desktop\CSSE 434\project\backend>node index.js
Example app listening on port 3000!
string

'hadoop' is not recognized as an internal or external command,
operable program or batch file.

_http_outgoing.js:371
    throw new Error('Can\'t set headers after they are sent.');
    ^

Error: Can't set headers after they are sent.
    at ServerResponse.setHeader (_http_outgoing.js:371:11)
    at ServerResponse.header (C:\Users\Administrator\Desktop\CSSE434\project\backend\node_modules\express\lib\response.js:767:10)
    at ServerResponse.contentType (C:\Users\Administrator\Desktop\CSSE 434\project\backend\node_modules\express\lib\response.js:595:15)
    at ServerResponse.send (C:\Users\Administrator\Desktop\CSSE 434\project\backend\node_modules\express\lib\response.js:145:14)
    at C:\Users\Administrator\Desktop\CSSE434\project\backend\index.js:10:17
    at ChildProcess.exithandler (child_process.js:212:5)
   at emitTwo (events.js:106:13)
    at ChildProcess.emit (events.js:194:7)
    at maybeClose (internal/child_process.js:899:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:226:5)

as you can see, the output string shows that the stderr is a string type the hadoop is not recognized is normal, because I do not have hadoop on my local But I just cannot figure out why it does not send the result to my client. Could someone help me with this?

PS:This is the only file in my project.There is no other files to show

Well, I just need to return res.send(stderr); The error occurs when it try to send "hello world" after sending the stderr

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM