@WrRan
2016-11-09T16:56:18.000000Z
字数 1829
阅读 778
node.js
sails
The core Router in Sails is the main (but not ONLY) player responsible for routing requests.
It is not involved with HTTP, WebSockets, or other internet protocols directly-- instead, it emits
events on the sails
object (a Node EventEmitter) when a route should be bound, allowing flexibility
in hooks' implementations.
The core Router includes a latent Express instance which is used only for internal routing of requests,
and is not actually used by any application code in userland-- that's the job of hooks. It may, however,
be used by app-level unit tests, in order to run test suites without having to lift a server and occupy a network port.
At the time of this writing, the http
hook listens for bind
events emitted from the core Router
and binds them directly to an external instance of Express.
On the other hand, the sockets
hook defers to the core router, emitting a request
event whenever
it receives and interprets a new, appropriately-formatted, socket message. The core Router intercepts this
and routes the request using its known middleware bindings. (core middleware, blueprint aka "shadow" routes, and statically configured routes from the routes.js
config file in userland)
When an HTTP request hits the server, does it hit the Sails router before it hits the Express router?
OK.. what requests DO hit the Sails router?
What happens after an HTTP request hits the Express router?
When and how are the routes in your routes.js
file processed?
routes.js
is read by the userconfig
hook, which loads it into sails.config.routes
.sails.config.routes
is used by the Sails router at lifttime (to bind routes to the external Express router) AND at runtime (to detect matches in wildcard routes coming from other attached servers like the Socket.io interpreter)