使用Bluemix

时间:2016-05-25 23:01:53

标签: node.js ibm-cloud ibm-watson visual-recognition

经过深入研究,我决定使用Bluemix对​​图像进行分类和识别。

我有一个关于如何使用node.js运行时开始编程的初步问题。

我尝试按照this教程。但是,这只是代码片段。你如何运行它们并看到它在Bluemix环境中工作?

我的进步:
- 我在Bluemix中启动了node.js启动器应用程序 - 我添加了以下代码,app.js如下所示:

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

我正在尝试在Bluemix环境(实时编辑模式)中运行代码,而不是在本地运行。当我点击运行代码时,部署停止,我甚至无法找到使这种情况发生的代码行。当我访问该网页时,我收到以下错误:

  

404 Not Found:请求的路线('myvisualapp.mybluemix.net')不存在。

我不明白什么是错的以及如何调试代码。

作者级别:初学者

2 个答案:

答案 0 :(得分:1)

  1. 您需要在快递中“路由”(或至少拦截)客户端请求。现在,请求没有处理程序。为此目的使用app.get()调用
  2. 您的watson服务调用现在无法满足用户请求。您需要通过用户请求汇集它。
  3. 例如:

    app.get('/', function(req, res) {
    
    // invoke watson services
    // get the result.
    // write back the result through the response object, res
    
    }
    

答案 1 :(得分:1)

您可以在https://github.com/watson-developer-cloud/visual-recognition-nodejs查看演示代码并获得一个好的起点。

此外,从命令行可以看到使用

部署到bluemix中的应用程序的日志

$ cf logs YOURAPPNAME --recent

其中YOURAPPNAME是您推送到bluemix的应用程序的名称。您可以使用

获取名称

$ cf apps

如果你忘记了你使用的名字(这一直发生在我身上)。