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
This is because res.send() finishes sending the response.
When res.send('Hello');
is encountered it sends the response immediately. So next res.send
is not executed and you cannot see visitors. Commenting the first res.send
allows you to view the second one.
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 += '<p id="c">' + ( parseInt(d) + 1 ) + '</p>';
res.send(response);
}
})
});
(or just res.send("Hello<p id=...")
, but you get the point :)