2

After thorough researching, I decided to use Bluemix for classifying and recognizing images.

I'm have a starter question on how to begin programming using node.js runtime.

I tried to follow this tutorial. However, that is just snippets of code. How do you run them and see it working in the Bluemix environment?

My progress:
-I started the node.js starter application in Bluemix.
-I added the following code and the app.js looks like this:

    /*eslint-env node*/

//--------------------------------------------------------------------------
// node.js starter application for Bluemix
//--------------------------------------------------------------------------

// This application uses express as its web server
// for more info, see: http://expressjs.com
var express = require('express');

// cfenv provides access to your Cloud Foundry environment
// for more info, see: https://www.npmjs.com/package/cfenv
var cfenv = require('cfenv');

// create a new express server
var app = express();

// serve the files out of ./public as our main files
app.use(express.static(__dirname + '/public'));

// get the app environment from Cloud Foundry
var appEnv = cfenv.getAppEnv();

// start server on the specified port and binding host
app.listen(appEnv.port, '0.0.0.0', function() {
  // print a message when the server starts listening
  console.log("server starting on " + appEnv.url);
});

var watson = require('watson-developer-cloud');
var fs = require('fs');

/*var visual_recognition = watson.visual_recognition({
  username: '<username>',
  password: '<password>',
  version: 'v2-beta',
  version_date: '2015-12-02'
});*/

 var visualRecognition = watson.visual_recognition({
   version: 'v3',
   api_key: process.env.API_KEY || 'my api key',
   version_date: '2015-05-19'
 });

var params = {
  images_file: fs.createReadStream('./resources/car.png')
};

visualRecognition.classify(params, function(err, res) {
  if (err)
    console.log(err);
  else
    console.log(JSON.stringify(res, null, 2));
});

I'm trying to run the code in the Bluemix environment (live edit mode) and not locally. When I hit run the code, the deployment stops and I can't even locate what line of code is making this happen. When I visit the webpage I get the following error:

404 Not Found: Requested route ('myvisualapp.mybluemix.net') does not exist.

I don't understand what's wrong and how to debug the code.

Author level: beginner

Joshua Smith
  • 6,561
  • 1
  • 30
  • 28
skyrocket
  • 41
  • 1
  • 5

2 Answers2

1
  1. You need to 'route' (or at least intercept) the client requests in express. Right now, the request do not have a handler. Use app.get() call for that purpose
  2. Your watson service calls are unbounded to a user request right now. You need to funnel it through a user request.

For example:

app.get('/', function(req, res) {

// invoke watson services
// get the result.
// write back the result through the response object, res

}
Gireesh Punathil
  • 1,344
  • 8
  • 18
1

You can look at the demo code at https://github.com/watson-developer-cloud/visual-recognition-nodejs and get a good place to start.

Also, from the command line you can see the logs of your application deployed into bluemix using

$ cf logs YOURAPPNAME --recent

where YOURAPPNAME is the name of the application you pushed to bluemix. You can get the name using

$ cf apps

if you forget the name you used (which happens to me all the time).

Joshua Smith
  • 6,561
  • 1
  • 30
  • 28