C
C
CoCoCoder2019-10-31 16:30:38
Node.js
CoCoCoder, 2019-10-31 16:30:38

Why does NodeJS loop through a function twice?

In general, there is such a simple code:

const http = require('http');
function counter() {
    let count = 0;
    return function() {
        ++count;
        return count;
    }
}
let countAlert = counter();
http.createServer(function(request,response){
    response.end(`Количество посещений сайта:${countAlert()}`);
}).listen(3000,'127.0.0.1',function() {
    console.log('Подключение выполнено успешно');
})

I didn’t upload it to the compiler site, because everything seems to be clear here anyway. However, there is a question why NodeJS goes through the function twice and instead of returning +1 attendance to me every time the page is updated, it does it +2?
That is, I have attendance in the following sequence:
1->3->5->7->...->2n+1
Why is this happening?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lynn "Coffee Man", 2019-10-31
@CoCoCoder

Open your browser console and take a look.
Or log `request.url`.
Because the browser actually makes the second request for `favicon.ico`.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question