Express - res.send() works once

后端 未结 2 1359
灰色年华
灰色年华 2021-02-20 05:59

I\'m new to Node and Express, I was trying to make something with Express just to get started, then I faced this problem.

First res.send() works well, but t

2条回答
  •  萌比男神i
    2021-02-20 07:02

    res.send() is meant to be called just once.

    Try this instead:

    app.get('/', function(req,res) {
      var response = 'Hello';
      fs.readFile('counter.txt','utf-8', function(e,d) {
          if (e) {
            console.log(e);
            res.send(500, 'Something went wrong');
          }
          else {
            console.log(parseInt(d) + 1);
            fs.writeFile('counter.txt',parseInt(d) + 1);
            response += '

    ' + ( parseInt(d) + 1 ) + '

    '; res.send(response); } }) });

    (or just res.send("Hello

    , but you get the point :)

提交回复
热议问题