未捕获的语法错误:意外的标记{



我正在尝试编写一个chai测试,其中我所做的只是流式传输一些音频并得到一个简单的响应:{},由于某种原因,当我将fs流管道传输到req时,我会收到这个错误Uncaught SyntaxError: Unexpected token {,如果我删除了管道,但没有该流,则测试工作正常。

服务器代码:

router.post('/', function (clientRequest, clientResponse) {
    clientRequest.on('end', function () {//when done streaming audio
        console.log('im at the end>>>>>');
        clientResponse.setHeader('Content-Type', 'application/json'); //I've tried removing that: same result
        clientResponse.json({});
        clientResponse.end(); //I've tried removing that: same result
    }); //end clientRequest.on('end',)
});

测试代码:

var app = require('./app');
describe('server', function() {
    this.timeout(10000);
    it('should WORK!!!"', function (done){
        var req = chai.request(app).post('/speech');
        var readStream = fs.createReadStream('./test.wav');
        readStream.on('end',function(){
            console.log("readStream end>>>>>>>>>>>>>>>>>>>>>>");
            req.end(function (err, res) {
                console.log("req.end callback>>>>>>>>>>>>>>>");
                done();
            });
        });
        readStream.pipe(req);
    });
});

错误:

Uncaught SyntaxError: Unexpected token {
     at Object.parse (native)
     at _stream_readable.js:908:16

错误分析

一般来说,代码实际上是有效的,而问题就在超级代理内部。

事实上,这个问题实际上缺少了一些细节,所以我不得不猜测缺少的部分,比如chai.request(app)是使用chai-http完成的,而chai-http又使用超级代理来执行http请求。

问题似乎出在超级代理内部,我可以用更多的信息重现你的错误(不确定为什么我得到了更长的跟踪):

Uncaught SyntaxError: Unexpected token {                                                                                                                      
 at Object.parse (native)                                                                                                                                     
 at IncomingMessage.<anonymous> (/project/path/node_modules/chai-http/node_modules/superagent/lib/node/parsers/json.js:9:2
 at IncomingMessage.EventEmitter.emit (events.js:117:20)                                                                                                      
 at _stream_readable.js:920:16                                                                                                                                
 at process._tickCallback (node.js:415:13) 

我能够检查JSON解析是否试图解析双重响应。类似于如果服务器用{}响应,则解析器有{}{},或者如果服务器用{"a":"b"}响应,则分析器有{"a":"b"}{"a":"b"}。您可以在此行之前插入console.log(res.text)来检查这一点(本地此文件位于node_modules/chai http://node_modules/superagent下)。

此外,如果我将readStream.pipe(req);移动到具有var readStream:的行的正上方

var readStream = fs.createReadStream('./test.wav');
readStream.pipe(req);
readStream.on('end',function(){
    ...

然后测试通过,但输出"double callback!"-它也被超级代理打印出来,并确认它出了问题。

修复

没有chai-httpsuperagent做同样的事情并不复杂。

首先,服务器端代码。它被稍微更改了一点——而不是clientRequest.on('end', ...,我将其管道传输到写入流。通过这种方式,我还可以检查文件是否被实际传输:

var express = require('express');
var fs = require('fs');
var app = express();
module.exports = app;
app.post('/speech', function (clientRequest, clientResponse) {
    console.log('speech');
    var writeStream = fs.createWriteStream('./test_out.wav');
    //for me on('end'... doesn't work (it infinitely waits for event
    //probably because the file is small and it finishes before we
    //get here
    clientRequest.pipe(writeStream).on('finish', function() {
        console.log('im at the end>>>>>');
        clientResponse.json({'a':'b'});
    });
});
app.listen(3000, function() {
  console.log("App started");
});

测试:

var fs = require('fs');
var chai = require('chai');
var http = require('http');
// Require our application and create a server for it
var app = require('./unexpected');
var server = http.createServer(app);
server.listen(0);
var addr = server.address();
describe('server', function() {
    this.timeout(10000);
    it('should WORK!!!"', function (done){
        // setup read stream
        var readStream = fs.createReadStream('./test.wav');
        readStream.on('end',function(){
            console.log("readStream end>>>>>>>>>>>>>>>>>>>>>>");
        });
        // setup the request
        var request = http.request({
            'host': 'localhost',
            'port': addr.port,
            'path': '/speech',
            'method': 'POST'
        });
        // now pipe the read stream to the request
        readStream.pipe(request).on('finish', function() {
            console.log("pipe end>>>>>>>>>>>>>>>>>>>>>>");
        });
        // get the response and finish when we get all the response data
        request.on('response', function(response) {
            console.log("request end>>>>>>>>>>>>>>>>>>>>>>");
            response.on('data', function(data) {
                console.log('response data: ' + data);
            });
            response.on('end', function(data) {
                console.log('done!');
                done();
            });
        });
    });
});

我认为代码应该是不言自明的,我只是使用标准节点http模块来完成这项工作。

您可以尝试替换此命令

clientResponse.json({});

带有

clientResponse.send({});

相关内容

  • 没有找到相关文章

最新更新