我在浏览器中运行我的 js 应用程序时遇到问题。我已经创建了客户端-服务器应用程序,并且正在使用Zipkin来跟踪它们之间的通信。
这是使用 Node.js 的客户端require()
:
const {Tracer, BatchRecorder, ExplicitContext} = require('zipkin');
const {HttpLogger} = require('zipkin-transport-http');
const {restInterceptor} = require('zipkin-instrumentation-cujojs-rest');
const rest = require('rest');
const ctxImpl = new ExplicitContext();
const recorder = new BatchRecorder({
logger: new HttpLogger({
endpoint: 'http://localhost:9411/api/v1/spans'
})
});
const tracer = new Tracer({ctxImpl, recorder});
const nameOfClient = 'cujojs-client';
const client = rest.wrap(restInterceptor, {tracer, serviceName: nameOfClient});
client({
method: 'GET',
path: 'http://localhost:9999/request'
}).then(function(response) {
console.log('response: ', response);
});
我正在使用 Browserify 捆绑依赖项以在浏览器中使用。
我跑来browserify CujojsClient.js -o bundle.js
为浏览器创建包。
如果我使用--node --ignore-missing
选项运行 browserify 一切都在 Node.js 中运行良好,但是当我在浏览器中运行包时(Windows 上的 Firefox 45.3.0)我只得到:
SyntaxError: missing : after property id
这是有问题的部分:
class HttpLogger {
constructor({endpoint, httpInterval = 1000}) {
this.endpoint = endpoint;
this.queue = [];
const timer = setInterval(() => {
this.processQueue();
}, httpInterval);
if (timer.unref) { // unref might not be available in browsers
timer.unref(); // Allows Node to terminate instead of blocking on timer
}
}
logSpan(span) {
this.queue.push(span.toJSON());
}
processQueue() {
if (this.queue.length > 0) {
const postBody = JSON.stringify(this.queue);
fetch(this.endpoint, {
method: 'POST',
body: postBody,
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
}
}).then((response) => {
if (response.status !== 202) {
console.error('Unexpected response while sending Zipkin data, status:' +
`${response.status}, body: ${postBody}`);
}
this.queue.length = 0;
}).catch((error) => {
console.error('Error sending Zipkin data', error);
this.queue.length = 0;
});
}
}
}
我的index.html
:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="bundle.js"></script>
</head>
</html>