user2979409
user2979409

Reputation: 791

Script output in NodeJS

I have the following code:

app.get('/scr', function(req, res) {
    var command = spawn(scripts + "/script.sh");
    command.stdout.on('data', function (data) {
        res.send(data);
    });
});

The script outputs Hello World!. I would like to have this output on my browser when going to http://localhost:XXXX/scr. However, this wants me to download a file with Hello World! inside. How can I obtain that output on my browser?

Upvotes: 0

Views: 35

Answers (2)

Brad
Brad

Reputation: 163301

You need to set a content type.

res.set('Content-Type', 'text/plain');

Upvotes: 0

icktoofay
icktoofay

Reputation: 128991

It’s likely that the data event will be fired multiple times, but send can only be called once. Rather than using send, use write and end; or, since command.stdout is a stream, just pipe it into the response:

command.stdout.pipe(res);

You may want to explicitly set the MIME type before that, though, e.g.:

res.type('text/plain');

Upvotes: 2

Related Questions