简体   繁体   中英

How to inject Models, Services in all Controllers by default while creating a node js app with express

I have a little experience with Sails and I really like the way it automatically injects all the models and services inside controllers so we save ourself from requiring all those at the beginning of the script, but the framework is very bulky.

So, I am making a project using express framework and want to implement this feature where I do not need to require Services and Models in each and every Controller script.

Thanks.

you can create a index file for exporting all services and models. Then just include it into your controllers

in service.js

exports = {
    auth    : require('./auth'),
    user    : require('./user'),
    and many more
}

in controller

var service = require('service')

use services as service.auth, service.user

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM