Skip to content

Instantly share code, notes, and snippets.

@mikermcneil
Last active August 29, 2015 14:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikermcneil/2ed1cb5d47a4e937a059 to your computer and use it in GitHub Desktop.
Save mikermcneil/2ed1cb5d47a4e937a059 to your computer and use it in GitHub Desktop.
share-able/replace-able parts of sails core

Ironically, Sails is actually becoming less and less MVC focused internally (see https://github.com/sails101/like-express for an example of what you can do now) On a separate note, I've been gradually working on making the Express dep optional in sails core for a while now. Features like views/static middleware/the orm/etc should be swappable IMO, e.g. you might only need/want socket.io. That was the whole idea of the "hooks" thing in 0.9 (original proposal.

So anyways I thought it might be useful to list how I'm applying Express 3 in Sails as a data point/use case:

Nowadays, Sails core (minus the cli) is essentially:

1. router

aka "request interpreter"

https://github.com/balderdashy/sails/tree/master/lib/router

(currently uses express, could use routification, but it is not HTTP specific-- you can bind routes w/ app.post() etc. and then simulate requests using app.request())

2. app

https://github.com/balderdashy/sails/tree/master/lib/app This takes care of basic default config, and uses @dominictarr's rc module (also has some utility functions) Would love to see this simplified into a thing that lets you extend the req and res objects (currently I'm extending them using middleware injected in hooks e.g. function _addResViewMethod(req,res,next){ res.view=function(){...}; next(); })

3. hook loader

https://github.com/balderdashy/sails/tree/master/lib/hooks (this is the thing that loads the hooks)

Worth mentioning is that the http hook actually builds an express app and can "lift" it on a port, whereas the router mentioned above is completely protocol-agnostic.

Then there are 20-odd hooks that are built in to core that I haven't taken the time to pull out into separate modules-- they can be overridden, and additional hooks can be added programmatically or using the .sailsrc file. They're like plugins. In many cases, hooks correspond loosely with middleware, but they also get an initialize(cb) function where they can tap into the framework boot. The hooks built in to the framework implement various pieces of the core MVC+sockets+pubsub functionality in Sails. Consequently, I'd say they represent the only part of our setup that is really MVC-only, or "sails"-y if you will. Some hooks are tiny and just call out to other modules (like "grunt"), while other hooks are much larger (like "orm", even though it calls out to waterline.) With waterline2 (let me know if you want me to add you to play around w/ it) it'll be considerably cleaner. https://github.com/balderdashy/sails/blob/master/lib/app/configuration/defaultHooks.js

@mikermcneil
Copy link
Author

Here are the default hooks as of v0.10:

  • moduleloader
  • logger
  • request
  • orm
  • views
  • blueprints
  • responses
  • controllers
  • sockets
  • pubsub
  • policies
  • services
  • csrf
  • cors
  • i18n
  • userconfig
  • session
  • grunt
  • http
  • userhooks

(they should all be separate node modules)

@mikermcneil
Copy link
Author

early but here's a link to the WIP inventory list https://gist.github.com/mikermcneil/dbe0bed18675c294da84

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment