I’m learning Java web development and have a rough picture of a Java app running on Tomcat. In a JavaScript stack, where does Node.js fit, and what actually handles browser requests?
So what’s the difference between Tomcat and Node.js?
Answers
They’re different kinds of things. Node.js provides an environment for running JavaScript outside a browser. Tomcat is a web server and servlet container that runs on the Java runtime. The comparison is closer to Node.js versus the Java runtime on one side, and Tomcat versus a Node web server on the other.
With Node, your application can create an HTTP server directly, or use a framework such as Express to handle routing and requests. In either case, a Node process is running the code that listens for and responds to requests.
Reply to this answer
A simple way to picture it: the runtime is what lets your program run, while the server is a program that listens for network requests and sends responses. Node.js lets JavaScript run on a machine or server. Your JavaScript application can then start a server using Node’s built-in HTTP features or a framework.
Tomcat is more specialized. It runs Java web components and provides web-server functionality. So the labels describe different roles, and a single product or process can cover more than one role.
Reply to this answer
For a basic stack diagram, you could write: browser sends a request, a Node.js application receives it, application code decides what to do, and it returns a response. A framework like Express can make the request-handling part easier, but it still runs on Node.js.
Node is often called a runtime because that is its core job. Calling it a server can still make sense informally when people mean the Node process running a web server. The wording depends on whether they mean the runtime or the application built with it.
Reply to this answer
One small correction to the original mental model: Tomcat itself also runs on Java, so it depends on a runtime rather than replacing one. Tomcat adds web-specific services, including handling servlet requests. Node.js gives you the JavaScript runtime and networking tools, but your app supplies or chooses the web-server layer.
Also, JavaScript’s usual execution model does not mean Node can only handle one request at a time. Its asynchronous I/O is designed to manage many waiting network operations. Whether a particular app handles its workload well depends on its code and setup.
Reply to this answer
Add your answer
Answers are reviewed before publication.
Related Discussions