在Loopback4中使用没有类的存储库?

时间:2019-12-10 12:45:38

标签: typescript loopback loopback4

我使用环回中的express创建了一个单独的路由器。当我从邮递员那条路线走时,它起作用了。我创建了一个单独的服务类,该类具有一个从db获取数据的功能。现在,当我创建该服务类的实例时,我需要传递存储库的引用,但是问题是我在路由器文件中没有该存储库的引用。那我该怎么做呢?

express-app.ts的代码

Answertype: 1
Timestamp: 2019-3-4
Values | EnglishLabel         | German Label
1      | almost always untrue |...
2      | often untrue         |...
3      | sometimes true       |...
4      | often true           |...
5      | alomost always true  |...

Ansertype: 2
Timestemp: 2018-4-2
Values | EnglishLabel        | German Label
1      | 25 years or younger |...
2      | 26 years to 34 years|...
3      | older than 34 years |...

webhook-service.ts

import { Request, Response } from 'express';
var express = require('express')
const legacyApp = express();
const xero_webhook_key = 'XXXXXXX'
const bodyParser = require('body-parser')
const crypto = require('crypto')
import { WebhookService } from './services/webhookService'
var options = {
  type: 'application/json'
};

// Using the options above, create a bodyParser middleware that returns raw responses.
var itrBodyParser = bodyParser.raw(options)
// your existing Express routes
legacyApp.post('/webhook', itrBodyParser, function (req: Request, res: Response) {

  // Create our HMAC hash of the body, using our webhooks key
  let hmac = crypto.createHmac("sha256", xero_webhook_key).update(req.body.toString()).digest("base64");
  new WebhookService().fetchData(req.body) //Here i need to pass repositry reference.
  console.log("Resp Signature: " + hmac)
  if (req.headers['x-xero-signature'] == hmac) {
    res.statusCode = 200
  } else {
    res.statusCode = 401
  }
  console.log("Response Code: " + res.statusCode)
  res.send()
});
legacyApp.get('/pug', function (_req: Request, res: Response) {
  res.send('Pug!');
});


export { legacyApp };

0 个答案:

没有答案