In PHP (or Java/ASP.NET/Ruby) based webservers every client request is instantiated on a new thread. But in Node.js all the clients run on the same thread (they can even share the same variables!) I understand that I/O operations are event-based so they don't block the main thread loop.
What I don't understand is WHY the author of Node chose it to be single-threaded? It makes things difficult. For example, I can't run a CPU intensive function because it blocks the main thread (and new client requests are blocked) so I need to spawn a process (which means I need to create a separate JavaScript file and execute another node process on it). However, in PHP cpu intensive tasks do not block other clients because as I mentioned each client is on a different thread. What are its advantages compared to multi-threaded web servers?
Note: I've used clustering to get around this, but it's not pretty.
node -e 'setTimeout(()=>{},1000);' & ps -T h $! | wc -l; kill $!
displays five threads on my system. The main event loop is single-threaded (it wouldn't make much sense if it wasn't) but Node is heavily multi-threaded and you can write multi-threaded single-process applications if you want. I would love to write a comprehensive answer about it but some people decided to close your question so I can't. I'm voting to reopen it. If it gets more votes and gets reopened then please mention me in the comment. – Inshrine