IBM Watson音调分析器发布请求问题

时间:2017-11-21 21:29:45

标签: node.js ibm-watson tone-analyzer

我是学生,学习编码和社区帮助表示赞赏。我已使用域名https://depionicapp.herokuapp.com/在heroku中托管我的代码。当我与邮递员进行邮寄请求时,我收到错误500但是在云9中我得到了结果。

var http = require('http');
var bodyParser = require("body-parser")
var vision = require('@google-cloud/vision');
// Authorizing on a per-API-basis. You don't need to do this if you auth on a 
// global basis (see Authorization section above). 
var express = require('express');
var router = express();

var visionClient = vision({
  projectId: 'ionic-face-api',
  keyFilename: './ionic.json'
});

router.use(bodyParser.urlencoded({extended: false }));
router.use(bodyParser.json());

router.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});

var server = http.createServer(router);
router.post('/info', function(req, res) {
  var text = req.body.text;
  console.log(text)
  var ToneAnalyzerV3 = require('watson-developer-cloud/tone-analyzer/v3');

  var tone_analyzer = new ToneAnalyzerV3({
    username: 'username',
    password: 'password',
    version_date: '2016-05-19'
  });

  tone_analyzer.tone({
      text: text
  }, function(err, tone) {
    if (err)
     console.log(err);
    else
     res.send((JSON.stringify(tone, null, 2)));
  });
});

邮差:

Postman request: https: //depionicapp.herokuapp.com/info
Content - Type: application / json 
Body I am sending from postman
{"text": "I am good.....blah blah..." }// Error 500

1 个答案:

答案 0 :(得分:1)

所以,试着帮助你。我编辑你的代码删除视觉模块,只需在Node.js中创建一个服务器来测试你的代码,效果很好。

文件 toneExample.js:

var http = require('http');
var bodyParser = require("body-parser");
var express = require('express');
var router = express();

router.use(bodyParser.urlencoded({extended: false}));
router.use(bodyParser.json());

router.post('/info', function(req, res) {
    var text = req.body.text;
    console.log(text)
    var ToneAnalyzerV3 = require('watson-developer-cloud/tone-analyzer/v3');

    var tone_analyzer = new ToneAnalyzerV3({
     username: 'yourUsername',
     password: 'yourPassword',
     version_date: '2016-05-19'
     });

    tone_analyzer.tone({
        text: text
    }, function(err, tone) {
    if (err) console.log(err);
    else res.send((JSON.stringify(tone, null, 2)));
    })
});

module.exports = router;

文件 server.js:

var server = require('./toneExample.js');
var port = process.env.PORT || process.env.VCAP_APP_PORT || 3000;

server.listen(port, function() {
  console.log('Server running on port: %d', port);
});

执行node server.js后,我在POSTMAN中为/info端点做了一个示例,我得到了成功结果:

enter image description here

<强>信息

  • 节点版本:6.11.2
  • 已安装的模块:expressbody-parserwatson-developer-cloud

Obs。:如果你使用version_date: 2017-09-21发布音调ID的返回结果:愤怒,恐惧,快乐,悲伤,分析,自信和暂定。并且返回结果仅适用于分数符合0.5 最小阈值的音调。

Official API Reference中查看更多相关信息。我建议你尝试检查你需要发送的标题,你可以在你的身体中使用的类型post,还有类似的东西。