#Nodejs event loop code#In the case of http.createServer(), it is a mix of Javascript and native code in the nodejs http library that make that work. Instead, the code that implements these objects or the TCP functions that they use must maintain a list of callbacks that are registered and when those events occur, it must execute code so that the appropriate callbacks are called and passed the appropriate data. This behavior is not built into Javascript. One missing piece here is that things like the server object you created allow you to register a callback that will be called one or more times in the future when some particular events occur. It leaves it running so that it can receive these future events when they occur. When that is the case, node.js runs the initial script to completion, but because there are now lasting objects that were created and are listening for events (in your case, a server), nodejs does not shut down the app. In other node.js applications, the initial script starts timers or servers or something like that which will receive events in the future. In some node.js applications, it runs that initial script and then exits (done with its work). It isn't really related to the event queue at all. Loading, parsing and executing the script file passed to node when it starts up is the task given to node.js. In your particular example, that will cause the function main to get parsed and then will cause the execution of main() which will run that function. It loads that script file into memory, parses the Javascript in it and executes it. When node.js starts up, it is given an initial script file to execute. Run Arbitrary Code While Waiting For Callback in Node?Īlso how does the main function get called relative to the main event Here's a related answer that might also help: It provides a multi-platform way of doing evented, async I/O that was developed for node.js, but is also being used by some other projects. Node.js itself uses libuv for the event loop. If Javascript is busy executing something, then it queues that event so that as soon as Javascript is no longer busy, it can then trigger the next event in the queue. The system surrounding the Javascript implementation in node.js keeps track of externally triggered events (timers, networking results, etc.) and when Javascript is not busy executing something and an external event occurs, it then triggers an associated Javascript callback. One interesting indicator of this is that setTimeout() is not actually part of ECMAScript, but rather something made available to the Javascript environment by the host. It isn't fundamental to Javascript itself so it's not part of the actual JS runtime. The event queue is built into the operating environment that hosts the Javascript interpreter. Where is the event queue? Is it in the javascript somewhere or is it Where is the event queue? Is it in the javascript somewhere or is it built into the interpreter? Also how does the main function get called relative to the main event loop? I understand the server.listen is attaching a handler to the event queue and that we are adding the callback similar to the c++ example. Send the response body as "Hello World" In a c++ application you would do something along the lines of: int main()) I am new to node.js and I am trying to wrap my head around how Node.js does it. I have seen similar questions on stack overflow but none of them fully dive down into the question that I have? I am familiar with event queues, how they work as well as implementing them.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |