-1

From node.js's server.js:

var server = http.createServer( function( argRequest, argResponse )
                                {
                                    console.log("createServer: We got a hit @ " + new Date());          
                                    var filePath = ""
                                    if( argRequest.url === '/')
                                    {
                                        filePath = './public/index.html'
                                    }
                                    else
                                    {
                                        filePath = './public' + argRequest.url
                                    }

                                    serveStatic( argResponse, filePath )
                                }
                            )

server.listen( 3000, function()
                     {
                        console.log("\nlisten: Server listening on port 3000!\n")
                     }

Output of the above Nodejs code:

:~/Documents/a_mean/githib$ nodemon server.js 
[nodemon] 2.0.6
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node server.js`

listen: Server listening on port 3000!

createServer: We got a hit @ Thu Feb 11 2021 11:27:36 GMT+0530 (... Standard Time)
./public/index.html
createServer: We got a hit @ Thu Feb 11 2021 11:27:36 GMT+0530 (... Standard Time)
./public/stylesheets/style.css
createServer: We got a hit @ Thu Feb 11 2021 11:27:36 GMT+0530 (... Standard Time)
./public/socket.io/socket.io.js
createServer: We got a hit @ Thu Feb 11 2021 11:27:36 GMT+0530 (... Standard Time)
createServer: We got a hit @ Thu Feb 11 2021 11:27:36 GMT+0530 (... Standard Time)
./public/javascripts/chat.js
./public/javascripts/chat_ui.js

The output is showing separate calls for js, css, and html files. Is this expected behaviour? Does this not mean that server is getting created repeatedly?

nodejs - http.createServer seems to call twice
This talks about 'favicon' being fetched but I don't see any such thing in my output.

What is happening here?

Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411

2 Answers2

3

The output is showing separate calls for js, css, and html files. Is this expected behaviour?

Yes. With HTTP 1.x, the client has to ask for each URL explicitly.

Does this not mean that server is getting created repeatedly?

No. It means that the callback function you pass to createServer is called every time there is a request event.


For comparison:

document.querySelector("button")
    .addEventListener("click", () => alert("Clicked"));
<button>Click Me</button>

() => alert("Clicked") is called every time there is a click, but addEventListener is called only once.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
-1

This is the expected behavior. What is happening here is like this: you create a server using an http.createServer function and pass a callback in it. what happened this callback is not invoked immediately. So here is the question who invoke this callback?

Next what you are doing is you are listening to events on port 3000 using a listen function.

Let go back to the question of who triggers the callback? Node is a runtime and it's event-driven. so, whenever there is an HTTP event it triggers the callback. So Nodejs is responsible for calling the function(callback) every time there is some event.

Here is a link for exploring the internal of nodejs https://www.smashingmagazine.com/2020/04/nodejs-internals/

osama
  • 116
  • 1
  • 9