0

I'm just about done reading "Node.js in Action", and I'm trying to put together the pieces of Node.js --> Connect --> Express. I have a question about the "servers" that we create in Node.

node_server = http.createServer();
connect_app = Connect();
express_app = Express();

In the code above, is it true that connect_app is basically a "subclass" of node_server? (I know, this is JavaScript, so we don't really have subclassing, but I don't know what else to call it; extension?). And likewise express_app is basically a "subclass" of connect_app? It's my understanding that all of these objects are servers which could be bound to a port and respond to requests, but that in practice we typically only bind ONE of them to a port and use it to proxy requests to other server objects.

Am I on the right track in learning this?

aynber
  • 22,380
  • 8
  • 50
  • 63
loneboat
  • 2,845
  • 5
  • 28
  • 40

1 Answers1

0

First of all, shake off the idea that there are 3 running servers - because there's only one.

Express is a framework that relies on Connect, which is another framework/set of middlewares. Further, Connect relies on the NodeJS's API (HTTP module). Basically an abstraction, one on top of another.

An analogy is that Express is a car, Connect is like an engine, NodeJS is the engine parts. You only have one running car (one server in your case), but multiple components powering it.

@josh3736 Has commented a better explanation how it works.

Community
  • 1
  • 1
Joseph
  • 117,725
  • 30
  • 181
  • 234
  • 1
    Right. I understand that they're not really three separate running servers - that's why I put "server" in quotes in my title. I was more interested in knowing if the pattern of creating multiple "servers" or "apps", but only binding ONE to a port (and letting it proxy requests to the others) was normal. Thanks for your answer! – loneboat Oct 17 '13 at 14:46
  • 2
    Well, they ***are*** all running at once, but you don't create an instance of each library; it happens implicitly when you instantiate Express. Express creates an internal instance of Connect, which creates an internal instance of node's http, which creates an internal instance of net. Each library provides additional functionality on top of the last. – josh3736 Oct 17 '13 at 14:48
  • @loneboat As far as I know, you can't use one port for multiple apps. However, there's [this answer](http://stackoverflow.com/questions/10791309/sharing-one-port-among-multiple-node-js-http-processes) where a module can be used to route requests from one port to other ports, where your other servers are running. – Joseph Oct 17 '13 at 14:50
  • @josh3736 What you have there is a better way to explain the issue. – Joseph Oct 17 '13 at 14:51
  • 1
    @JosephtheDreamer : Stop being so agreeable - you're doing the Internet wrong! :-D – loneboat Oct 17 '13 at 15:02