Loopbackjs 如何在环回中间件中发布评估数据?

Loopbackjs 如何在环回中间件中发布评估数据?,loopbackjs,strongloop,loopback,Loopbackjs,Strongloop,Loopback,我想使用自定义API来评估应用程序发布的数据,但环回中间件中不接受远程方法 module.exports = function () { const http = require('https'); var request = require('request'); var { Lib } = require('Lib'); var lib = new Lib; verification.checkID = function (ID, cb) { cb(null,

我想使用自定义API来评估应用程序发布的数据,但环回中间件中不接受远程方法

module.exports = function () {
  const http = require('https');
  var request = require('request');
  var { Lib } = require('Lib');
  var lib = new Lib;

  verification.checkID = function (ID, cb) {
    cb(null, 'ID is :' + ID);
  }

  verification.remoteMethod('greet', {
    accepts: {
      arg: 'ID',
      type: 'string'
    },
    returns: {
      arg: 'OK',
      type: 'string'
    }
  });
更新

module.exports = function(server) {
  // Install a `/` route that returns server status
  var router = server.loopback.Router();
  router.get('/', server.loopback.status());

  router.get('/ping', function(req, res) { // your middle ware function now you need to call the next() here
    res.send('pong');
  });

  server.use(router);
};

要评估我没有得到的东西,请也检查这个链接

关于休闲问题

我以休闲的方式找到我的解决方案:

   module.exports = function(server) {
        const https = require('https');
        var request = require('request');

    return function verification(req, res, next) {
        res.setHeader('Access-Control-Allow-Origin', '*');
        res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
        res.setHeader('Access-Control-Allow-Headers', 'Content-Type'); 
        res.setHeader('Access-Control-Allow-Credentials', true);
            var request;
            var response;
            var body = '';
            // When a chunk of data arrives.
            req.on('data', function (chunk) {
                // Append it.
                body += chunk;
            });
            // When finished with data.
            req.on('end', function () {
                // Show what just arrived if POST.
                if (req.method === 'POST') {
                    console.log(body);
                }
                // Which method?
                switch (req.method) {
                    case 'GET':
                         Verify url and respond with appropriate data.
                         handleGet(req, res);
                         Response has already been sent.
                         response = '';
                         break;
                    case 'POST':
                        // Verify JSON request and respond with stringified JSON response.
                        response = handlePost(body);
                        break;
                    default:
                        response = JSON.stringify({ 'error': 'Not A POST' });
                        break;
                }
                // Send the response if not empty.
                if (response.length !== 0) {
                    res.write(response);
                    res.end();
                }
                // Paranoid clear of the 'body'. Seems to work without
                // this, but I don't trust it...
                body = '';
            });
            // If error.
            req.on('error', function (err) {
                res.write(JSON.stringify({ 'error': err.message }));
                res.end();
            });
            //
        };
        function handlePost(body) {
            var response = '';
            var obj = JSON.parse(body);
            // Error if no 'fcn' property.
            if (obj['fcn'] === 'undefined') {
                return JSON.stringify({ 'error': 'Request method missing' });
            }
            // Which function.
            switch (obj['fcn']) {
                // Calculate() requres 3 arguments.
                case 'verification':
                    // Error if no arguments.
                    if ((obj['arg'] === 'undefined') || (obj['arg'].length !== 3)) {
                        response = JSON.stringify({ 'error': 'Arguments missing' });
                        break;
                    }
                    // Return with response from method.
                    response = verification(obj['arg']);
                    break;
                default:
                    response = JSON.stringify({ 'error': 'Unknown function' });
                    break;
            }
            return response;
        };
        function verification(arg) {
            var n1 = Number(arg[0]);
            var n2 = Number(arg[1]);
            var n3 = Number(arg[2]);
            var result;
            // Addem up.
            result = n1 + n2 + n3;
            // Return with JSON string.
            return JSON.stringify({ 'result': result });
        };
        };

因此,您希望为所有帖子创建一个自定义方法或一个类似拦截器的钩子requests@RahulSingh实际上,我有一些非公共的模型,我希望通过添加中间件阶段对这些模型进行
CRUD
操作。您可以使用远程方法,并连接到非公共的模型,并执行所有操作那里为什么有一个中间件方法?@RahulSingh在上面的代码中,我无法从POST-to-process中获取数据并传递给私有模型。我已经用POST-Verb更新了答案,请注意。这是行不通的。首先,中间件应该接受我从应用程序发布的参数,然后发布到私有模型。我的问题在第一部分。我无法从应用程序发布(然后发送到控制台进行检查)。@EramSa您想编写一个中间件方法,它是一个快速方法,只需将其放在服务器/root.jsUpdated中即可。请检查更新的答案
   module.exports = function(server) {
        const https = require('https');
        var request = require('request');

    return function verification(req, res, next) {
        res.setHeader('Access-Control-Allow-Origin', '*');
        res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
        res.setHeader('Access-Control-Allow-Headers', 'Content-Type'); 
        res.setHeader('Access-Control-Allow-Credentials', true);
            var request;
            var response;
            var body = '';
            // When a chunk of data arrives.
            req.on('data', function (chunk) {
                // Append it.
                body += chunk;
            });
            // When finished with data.
            req.on('end', function () {
                // Show what just arrived if POST.
                if (req.method === 'POST') {
                    console.log(body);
                }
                // Which method?
                switch (req.method) {
                    case 'GET':
                         Verify url and respond with appropriate data.
                         handleGet(req, res);
                         Response has already been sent.
                         response = '';
                         break;
                    case 'POST':
                        // Verify JSON request and respond with stringified JSON response.
                        response = handlePost(body);
                        break;
                    default:
                        response = JSON.stringify({ 'error': 'Not A POST' });
                        break;
                }
                // Send the response if not empty.
                if (response.length !== 0) {
                    res.write(response);
                    res.end();
                }
                // Paranoid clear of the 'body'. Seems to work without
                // this, but I don't trust it...
                body = '';
            });
            // If error.
            req.on('error', function (err) {
                res.write(JSON.stringify({ 'error': err.message }));
                res.end();
            });
            //
        };
        function handlePost(body) {
            var response = '';
            var obj = JSON.parse(body);
            // Error if no 'fcn' property.
            if (obj['fcn'] === 'undefined') {
                return JSON.stringify({ 'error': 'Request method missing' });
            }
            // Which function.
            switch (obj['fcn']) {
                // Calculate() requres 3 arguments.
                case 'verification':
                    // Error if no arguments.
                    if ((obj['arg'] === 'undefined') || (obj['arg'].length !== 3)) {
                        response = JSON.stringify({ 'error': 'Arguments missing' });
                        break;
                    }
                    // Return with response from method.
                    response = verification(obj['arg']);
                    break;
                default:
                    response = JSON.stringify({ 'error': 'Unknown function' });
                    break;
            }
            return response;
        };
        function verification(arg) {
            var n1 = Number(arg[0]);
            var n2 = Number(arg[1]);
            var n3 = Number(arg[2]);
            var result;
            // Addem up.
            result = n1 + n2 + n3;
            // Return with JSON string.
            return JSON.stringify({ 'result': result });
        };
        };