像在其他流行的mvc框架中一样,如何在环回中具有服务层? [英] How to have service layer in loopback just like in other popular mvc frameworks?

查看:51
本文介绍了像在其他流行的mvc框架中一样,如何在环回中具有服务层?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始在sailsjs中构建应用程序,但是我决定将其移回环回.从j2ee/spring mvc的背景开始,我很快就使用sailsjs并在api/service中使用了一些业务逻辑来运行它.

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.

不幸的是,我还没有找到一种在环回时创建这些服务的方法.我不是在谈论远程方法.这些服务并没有真正绑定到任何模型,它们位于模型之上.我尝试在server/service/DataModelService.js

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")
   }
}

使用以下代码

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 ?

预先感谢

推荐答案

您需要require您要访问的模块.试试这个:

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());
  });
};

未经require()调用,该变量未定义.您可以通过这种方式在应用程序中的任何文件中require这项服务(它只是一个普通的Node模块).

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.

这篇关于像在其他流行的mvc框架中一样,如何在环回中具有服务层?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆