【发布时间】:2018-07-05 09:36:03
【问题描述】:
当我尝试运行我的节点 js 应用程序时出现此错误:
_http_client.js:127 throw new TypeError('请求路径包含非转义字符'); ^
TypeError:请求路径包含非转义字符 在新的 ClientRequest (_http_client.js:127:13) 在 Object.request (http.js:38:10) 在 Object.exports.getDuration (/Users/paulcarron/git/integration-test-runner/modules/getEventLog.js:53:20) 在 /Users/paulcarron/git/integration-test-runner/modules/jobRunningService.js:75:21 在传入消息。 (/Users/paulcarron/git/integration-test-runner/modules/getEventLog.js:35:13) 在 emitNone (events.js:111:20) 在 IncomingMessage.emit (events.js:208:7) 在 endReadableNT (_stream_readable.js:1056:12) 在 _combinedTickCallback (内部/进程/next_tick.js:138:11) 在 process._tickCallback (internal/process/next_tick.js:180:9)
经过一番调查,我已经得出结论,因为 getEventLog.js 中的 + myId (请参阅问题最底部的代码)。此函数在不同的脚本中调用整个脚本:
child.stderr.on('data', function(uuid, data) {
getEventLog.getId(uuid, function(err, id){
if(err) return console.log(err)
getEventLog.getDuration(id, function(err, duration){
if(err) return console.log(err)
jobLogger.info(escape(data) + "<br />");
})
});
});
我认为 myId 中可能有一个空格,所以我添加了一个 console.log("myId: " + myId) 并发现它返回了 myId: [object Object]。我该如何解决这个问题,以便 myId 是正确格式的 ID?
var http = require("http");
var fs = require('fs');
var async = require('async');
var readline = require('readline')
//var db = require('./dbPool');
//get file name
var options = {
"method" : "GET",
"hostname" : "127.0.0.1",
"port" : "18080",
"path" : "/api/v1/applications/"
};
exports.getId = function (uuid, callback) {
var req = http.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var body = JSON.parse(Buffer.concat(chunks));
var arrFound = Object.keys(body).filter(function(key) {
if (body[key].name.indexOf(uuid) > -1) {
return body[key].name;
}
}).reduce(function(obj, key){
obj = body[key].id;
return obj;
}, {});
callback(null, arrFound);
});
});
req.end();
}
exports.getDuration = function (myId, callback) {
//getEventLog.getId(function(err, id){
//get file name
var options = {
"method" : "GET",
"hostname" : "127.0.0.1",
"port" : "18080",
"path" : "/api/v1/applications/" + myId
};
var req = http.request(options, function (res) {
console.log(options)
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var body = JSON.parse(Buffer.concat(chunks));
var attempts = body.attempts
var arrFound = Object.keys(body).filter(function(key) {
return attempts[0].duration;
}).reduce(function(obj, key){
obj = body.attempts[0].duration;
return obj;
}, {});
//console.log(arrFound);
callback(null, arrFound);
});
});
req.end();
// })
};
【问题讨论】: