Browserify - 代码在节点中工作,但在捆绑后在浏览器中失败

时间:2016-08-22 08:59:42

标签: javascript node.js frontend browserify zipkin

我在浏览器中运行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>

0 个答案:

没有答案
相关问题