如何在环回的中间件中发布数据以进行评估?

时间:2017-10-07 19:44:25

标签: 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, 'ID is :' + ID);
  }

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

2 个答案:

答案 0 :(得分:1)

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', {
    'http': { // add the verb here
      'path': '/greet',
      'verb': 'post'
    },
    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);
};

要评估是我没有得到的,请检查此链接Intercepting error handling with loopback

答案 1 :(得分:0)

关于暂缓问题How to make a simple API for post method?

我找到了我的解决方案:

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