简体   繁体   中英

express http server when I send a request it throw me this error

I am very new to node.js, what I want to do is to call java program from node.js then return data back to client.

_http_outgoing.js:636
nextTick(msg.socket[async_id_symbol],
                   ^

TypeError: Cannot read property 'Symbol(asyncId)' of null
at write_ (_http_outgoing.js:636:24)
at ServerResponse.write (_http_outgoing.js:630:10)
at Socket.<anonymous> (/Users/Yiming/Sites/chartingProjectNode/http-server.js:18:13)
at emitOne (events.js:115:13)
at Socket.emit (events.js:210:7)
at addChunk (_stream_readable.js:252:12)
at readableAddChunk (_stream_readable.js:239:11)
at Socket.Readable.push (_stream_readable.js:197:10)
at Pipe.onread (net.js:588:20)

The code in the http-server.js

var express = require('express');
var app = express();

app.use('/JS',express.static(__dirname + '/JS'));
app.use('/CSS',express.static(__dirname + '/CSS'));
app.use('/', express.static(__dirname + '/'));

app.get('/', function (req, res) {
    res.sendFile("/index.html");
}).listen(8080);

app.get('/toJSON', function(req, res, next) {
    var files = req.query.files;
    var spawn = require('child_process').spawn;
    var child = spawn('java',  ['-cp', 'CSVExtractor.jar:.', 'toJSON.ToJSON', files]);
// prc.stdout.setEncoding('utf8');
    child.stdout.on('data', function (data) {
        res.write(data);
        res.end();
    });
    child.stderr.on('data', function (data) {
        process.stderr.write(data);
    });

});

The code in the javascript:

$.get("http://localhost:8080/toJSON?files=" + fileNames.join(), 
    function (d){
        console.log(d);
    }
);

I will sent file names to http-server.js, then call java program then use the output as the data.

It can only be run once, then it will throw me this error. The output of the first time:

{
"nodes":[
{"name" : "123", "group": 1, "inner_radius": 0},
{"name" : "456", "group": 2, "inner_radius": 2},
{"name" : "789", "group": 3, "inner_radius": 1}
],
"links":[
{"source": 0, "target": 1},
{"source": 0, "target": 2},
{"source": 1, "target": 0},
{"source": 2, "target": 0}
]
}

The data event can fire multiple times, but you're ending the response after the first one. Instead, end the response when the end event on the stream has fired:

child.stdout.on('data', function (data) {
    res.write(data);
}).on('end', function() {
    res.end();
});

(it would also be a good idea to add error event handlers)

It looks like, based on what was in the callstack, that the socket closes due to res.end() and then msg.socket[async_id_symbol] is called but the socket isn't open anymore.

TypeError: Cannot read property 'Symbol(asyncId)' of null
at write_ (_http_outgoing.js:636:24)

You could try commenting out res.end(); and see if it continues working.

https://nodejs.org/api/child_process.html#child_process_subprocess_stdout

childprocess.stdout is a readable stream, so res.end() might be killing it.

This also looks like it could be very helpful:

Event: 'end'# The 'end' event is emitted when there is no more data to be consumed from the stream.

Note: The 'end' event will not be emitted unless the data is completely consumed. This can be accomplished by switching the stream into flowing mode, or by calling stream.read() repeatedly until all data has been consumed.

const readable = getReadableStreamSomehow();
readable.on('data', (chunk) => {
  console.log(`Received ${chunk.length} bytes of data.`);
});

readable.on('end', () => {
  console.log('There will be no more data.');
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM