As mentioned by others, the Loopback documentation answers your question like this:
- Adding logic to models - adding remote methods, remote hooks and operation hooks.
- Defining boot scripts - writing scripts (in the /server/boot directory) that run when the application starts.
- Defining middleware - adding custom middleware to the application .
That's a great answer if you have custom functions for a particular model, boot script, or middleware. And as Dharmendra Yadav said, mixins can be another option:
You can use mixins to perform different common actions on models such as observing changes using operation hooks and adding model attributes.
But what about code that simply doesn't fit into any of those categories?
I don't have experience with a lot of web frameworks, but one framework I have used is Grails, which is very opinionated and gives you a place for just about everything. And if your code doesn't fit into any of those categories, they give you a place for that too:
- src/main/groovy - Supporting sources
So when I ran into this same problem in Loopback, I just created a src
directory under server
and that's where I put some helper classes that don't seem to fit anywhere else. And I include them as needed:
const HelperClass = require('../src/HelperClass');
HelperClass.helperFunction()...
Of course you can name the folder however you'd like: src
, helpers
, support
, whatever. And then put it under common
or server
as appropriate.