0

I except to do a simulation of the terminal in front and therefore to display live the messages of the sass --watch the problem is that res.render I can only do it once and what there is any way to return this multiple times?

So here my code :

app.get('/sass', function (req, res) {
  path1 = req.query.chemin;
  path2 = req.query.chemin;
  
  const ls = spawn('sass', ['--watch path1 path2'], { shell: true });

  ls.stdout.on('data', (data) => {
    
    console.log(ls.pid);
    message = JSON.stringify(`stdout: ${data}`);

    res.render('index', { messagee: message, path: firstpath });

  });
  
});

Front :

<div id="terminal"> <%= messagee %> </div>

My infrastructures :

server.js -views --index.ejs -public --css --js --img

Thank you for your answer

  • Shouldn't those be separate arguments, like `[ '--watch', 'path1', 'path2' ]`? – tadman Dec 06 '22 at 13:59
  • Yeah i know but i change for stack overflow – William Gaonarch Dec 06 '22 at 14:02
  • Why? Changing things like that just causes confusion. When you're having problems, don't create more of them. – tadman Dec 06 '22 at 14:03
  • Yeah i know but that work and it's not my question xD , my question it's for render the message multiple time on the front – William Gaonarch Dec 06 '22 at 14:10
  • Are you really asking [How do I stream a result?](https://stackoverflow.com/questions/38788721/how-do-i-stream-response-in-express). You either wait for the complete output, or you stream the data using `write`. – tadman Dec 06 '22 at 14:14
  • Nah because in the example it's all in 1 time but I want to send 1 time then change when a new response from the terminal , (maybe it's me who doesn't understand everything) – William Gaonarch Dec 06 '22 at 14:24
  • It's either "1 time" as in there is no new response, or it isn't. You're contradicting yourself here. Remember `on('data')` will emit multiple events by definition. You could use `pipe()` to aggregate that into a string before writing out the final response if you want. – tadman Dec 06 '22 at 14:34
  • Thank's man that works finaly tank you very very much :) – William Gaonarch Dec 06 '22 at 14:40

0 Answers0