0

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?

2
  • where is your browser code? Commented Oct 5, 2014 at 19:08
  • @lombausch: Probably here or here. More seriously, I think they’re asking a purely server-side question; the only part of the question related to the client was why the browser was prompting them to download it rather than just view it. Commented Oct 5, 2014 at 19:11

2 Answers 2

2

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');
Sign up to request clarification or add additional context in comments.

Comments

0

You need to set a content type.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.