未捕获到的SyntaxError:意外令牌{ [英] Uncaught SyntaxError: Unexpected token {
问题描述
我正在尝试编写一个chai测试,我所做的只是流一些音频并获得简单的响应: {}
,由于某种原因,我得到了此错误未捕获的语法错误:当我将
fs
流传送到 req <时,出现了意外的令牌{
/ code>,如果我移除管道并且没有该流,则测试工作正常。
I am trying to write a chai test where all I do is just stream some audio and get a simple response back: {}
, for some reason I'm getting this error Uncaught SyntaxError: Unexpected token {
when ever I pipe my fs
stream to req
, if I remove the piping and I don't have that stream the test works fine.
服务器代码:
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
推荐答案
错误分析
一般而言,ode实际上是可行的,问题出在超级代理内部。
Error analysis
The code in general is actually works and the problem is somewhere inside the superagent.
实际上,问题实际上缺少一些细节,因此我不得不猜测缺少的部分,例如 chai.request(app)
使用 chai-http ,然后依次使用 superagent 来执行http请求。
Actually question actually is missing some details, so I had to guess missing parts, like chai.request(app)
is done using chai-http, which in turn uses superagent to perform http requests.
问题似乎出在超级代理内部,我能够用更多的信息重现您的错误(不确定为什么我需要更长的跟踪时间):
And the problem seems to be somewhere inside the superagent, I was able to reproduce your error with a bit more info (not sure why I got longer trace):
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_modul下es / chai-http / node_modules / superagent)。
And I was able to check that JSON parses tries to parse double response. Like if server responses with {}
, the parser has {}{}
, or if server responses with {"a":"b"}
, the parser has {"a":"b"}{"a":"b"}
.
You can check this yourself inserting console.log(res.text)
before this line (locally this file is under node_modules/chai-http/node_modules/superagent).
此外,如果我移动 readStream.pipe(req);
在 var readStream
的行上方:
Also if I move the readStream.pipe(req);
just above the line with var readStream
:
var readStream = fs.createReadStream('./test.wav');
readStream.pipe(req);
readStream.on('end',function(){
...
然后测试通过,但是输出'double callback!'-也是由超级代理打印,并确认它出了问题。
Then the test passes, but outputs 'double callback!' - it is also printed by superagent and confirms that something goes wrong with it.
在没有 chai-http
和 superagent
的情况下做同样的事情并不复杂。
It is not complex to do the same thing without chai-http
and superagent
.
首先,是服务器端代码。它有所更改-代替了 clientRequest.on('end',...
我通过管道将其传输到写入流。通过这种方式,我还可以检查文件是否已真正传输:
First, the server-side code. It is changed a bit - instead of clientRequest.on('end', ...
I pipe it to the write stream. This way I also can check that the file was actually transferred:
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模块来完成这项工作。
I think code should be self-explanatory, I just use the standard node http module to do the job.
这篇关于未捕获到的SyntaxError:意外令牌{的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!