developit / express-es6-rest-api

:battery: Starter project for an ES6 RESTful Express API.
http://git.io/express-es6-rest-api
MIT License
2.46k stars 552 forks source link

Explanation on use of resource-router-middleware #25

Closed amberley closed 7 years ago

amberley commented 7 years ago

Would you mind giving a quick explanation of what resource-router-middleware accomplishes in this boilerplate? (Like why include it instead of regular routing) Perhaps I'm a bit confused also on the use of the word 'facet' in this instance. Much appreciated, I would like to understand.

developit commented 7 years ago

Facet is a silly word for this example, I keep meaning to change it but havent yet. It's just a collection of objects - normally you'd be using a database of some kind, we're just using an Array as a stand-in for a simple in-memory collection.

resource-router-middleware is used here because it promotes idiomatic REST - simple CRUD, guids for resources, etc. It's also a convenient way to automatically load a model instance (or fetch an object from a DB or something) for requests that will need access to it.

You can absolutely use this repo without resource-router-middleware, but I would still strongly recommend keeping the middleware semantic - export a factory function that returns an express Router so it can be instantiated and mounted as necessary. Without this, applications tend to devolve into singletons, which are much harder to re-use, test and refactor. You can see a direct example of the middleware factory approach here that doesn't use resource-router-middleware.

zouhir commented 7 years ago

If you have any further questions @amberley please feel free to re-open or ask at anytime!

I'll try my best to update the docs asap with explanation.

designbuedchen commented 6 years ago

@zouhir I really need an explanation to this. I really don't have an idea where to put this logically into. I would love to see an explanation to this 👍 Thanks in advance!