How to have service layer in loopback just like in other popular mvc frameworks?

雨燕双飞 提交于 2019-12-14 02:04:07

问题


I have started building an application in sailsjs but I decided to move it to loopback. From a j2ee/spring mvc background I was quickly up and running with sailsjs with some of my business logic in the api/service.

Unfortunatly I have not found a way to create those services on loopback. I am not talking about remote method. These services are not really tied to any model, they are on a layer above models. I have tried creating the following at server/service/DataModelService.js

module.exports = {
   testMethod: function(){
      return "Hello joseph"
   },
   testAnotherMethod: function(req,res){
       //lots of other processing etc. Calling other services etc
      res.send("something")
   }
}

Created server/boo/routes.js with this following

module.exports = function(app){
app.get('/test', function(req, res){
    res.send(DataModelService.testMethod());
});

}

but quickly got this reference error:

DataModelService is not defined
at /media/joseph/Data/Personal/tutorials/testingloopback/server /boot/routes.js:3:18
at Layer.handle [as handle_request] (/media/joseph/Data/Personal/tutorials/testingloopback/node_modules/loopback/node_modules/express/lib/router/layer.js:95:5)
at next (/media/joseph/Data/Personal/tutorials/testingloopback/node_modules/loopback/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/media/joseph/Data/Personal/tutorials/testingloopback/node_modules/loopback/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/media/joseph/Data/Personal/tutorials/testingloopback/node_modules/loopback/node_modules/express/lib/router/layer.js:95:5)
at /media/joseph/Data/Personal/tutorials/testingloopback/node_modules/loopback/node_modules/express/lib/router/index.js:277:22
at Function.process_params (/media/joseph/Data/Personal/tutorials/testingloopback/node_modules/loopback/node_modules/express/lib/router/index.js:330:12)
at next (/media/joseph/Data/Personal/tutorials/testingloopback/node_modules/loopback/node_modules/express/lib/router/index.js:271:10)
at cors (/media/joseph/Data/Personal/tutorials/testingloopback/node_modules/cors/lib/index.js:178:7)
at /media/joseph/Data/Personal/tutorials/testingloopback/node_modules /cors/lib/index.js:228:17

Can anyone show the right way of doing this ?

Thanks in advance


回答1:


You need to require the module you're trying to access. Try this:

// server/boot/routes.js
var DataModelService = require('../service/DataModelService.js');

module.exports = function(app){
  app.get('/test', function(req, res){
    res.send(DataModelService.testMethod());
  });
};

Without the require() call the variable is undefined. You can require this service (which is just a plain Node module) in any file in your application this way.



来源:https://stackoverflow.com/questions/35117050/how-to-have-service-layer-in-loopback-just-like-in-other-popular-mvc-frameworks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!