[英]Bluemix - Cloudant node.js: Error when calling API
I have a node.js app to call an API. 我有一个node.js应用程序来调用API。 The API works well on the first call, but on the second call, it returns this error message: 该API在第一个调用中运行良好,但是在第二个调用中,它返回以下错误消息:
404 Not Found: Requested route ('abc.mybluemix.net') does not exist.
Please help review the app.js function: 请帮助查看app.js函数:
app.js
app.get('/abc/:provider_id/staffs', function(request, response) {
console.log("Get method invoked.. ")
db = cloudant.use(dbCredentials.dbStaff);
//db = cloudant.use("staffs");
var docList = [];
db.list(function(err, body) {
if (!err) {
var len = body.rows.length;
console.log('total # of docs -> '+len);
if(len == 0) {
// error
} else {
var i = 0;
body.rows.forEach(function(document) {
db.search('allstaff', 'allstaff_index', {q:"provider_id:"+request.params.provider_id}, function(err, doc) {
if (!err) {
if(doc['_attachments']) {
// todo
} else {
var responseDataStaff = createResponseDataStaffs(
doc.rows[i].fields.id,
doc.rows[i].fields.provider_id,
doc.rows[i].fields.firstname,
doc.rows[i].fields.lastname,
doc.rows[i].fields.avatar,
doc.rows[i].fields.email,
doc.rows[i].fields.mobile,
doc.rows[i].fields.address,
doc.rows[i].fields.username,
doc.rows[i].fields.lastlogin,
doc.rows[i].fields.lastlogout
);
}
docList.push(responseDataStaff);
i++;
if(i >= doc.rows.length ) {
response.write(JSON.stringify(docList));
console.log('ending response...');
response.end();
}
} else {
console.log(err);
}
});
});
}
} else {
console.log(err);
}
});
The reason you get 404 on the second time is because your app crashed. 您第二次收到404的原因是因为您的应用程序崩溃了。 Debug it locally before you push to Bluemix. 在推送到Bluemix之前,先在本地对其进行调试。
To debug locally you need to have VCAP_SERVICES defined for your app: 要在本地调试,您需要为您的应用定义VCAP_SERVICES:
Open a terminal and type cf env 打开一个终端并输入cf env
Copy the content of VCAP_SERVICES to a local file (eg VCAP_SERVICES.json) 将VCAP_SERVICES的内容复制到本地文件(例如VCAP_SERVICES.json)
Create a new file next to app.js (eg debugApp.js) with this content 使用以下内容在app.js(例如debugApp.js)旁边创建一个新文件
if(!process.env.VCAP_SERVICES){
process.env.VCAP_SERVICES = JSON.stringify(require("./VCAP_Services.json"));
}
require('./app.js');
Then run node debugApp.js
然后运行node debugApp.js
I'm not sure what you're trying to achieve here but it looks bad 我不确定您要在这里实现什么目标,但看起来很糟糕
I'm guessing that this isn't what you intended to do. 我猜这不是您想要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.