Reputation: 11
I am student and learning to code and help from community is appreciated. I have hosted my code in heroku with domain name https://depionicapp.herokuapp.com/. When I am doing post request with postman I am getting Error 500
but with cloud 9 I am able to get the result.
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:
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
Upvotes: 1
Views: 482
Reputation: 5340
So, trying to help you. I edit your code deleting the vision module and just create one server in Node.js for test your code, works perfectly.
File 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;
File 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);
});
After executing node server.js
, I did one example in POSTMAN for the /info
endpoint and I get the results successful:
Info:
express
, body-parser
, watson-developer-cloud
Obs.: If you use the version_date: 2017-09-21
the return results for the post tone IDs: anger, fear, joy, sadness, analytical, confident, and tentative. And the returns results only for tones whose scores meet a minimum threshold of 0.5
.
See more about in the Official API Reference. I recommend to you try to check the headers that you need to sent, the type of you can use in your body post
, some stuffs like that.
Upvotes: 1