Nel codice seguente non riesco a capire perché req.pipe (res) non funzioni, e tuttavia non genera neanche un errore. Un'intuizione mi dice che è dovuto al comportamento asynch del nodo, ma questo è un caso molto semplice senza callback.NodeJS - Come eseguire lo streaming del corpo della richiesta senza buffering
Cosa mi manca?
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('Echo service: \nUrl: ' + req.url);
res.write('\nHeaders:\n' + JSON.stringify(req.headers, true, 2));
res.write('\nBody:\n');
req.pipe(res); // does not work
res.end();
}).listen(8000);
Ecco il riccio:
➜ ldap-auth-gateway git:(master) ✗ curl -v -X POST --data "test.payload" --header "Cookie: token=12345678" --header "Content-Type:text/plain" localhost:9002
ecco l'output di debug (vedere che il corpo è stato caricato):
About to connect() to localhost port 9002 (#0)
Trying 127.0.0.1...
connected
Connected to localhost (127.0.0.1) port 9002 (#0)
POST/HTTP/1.1
User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8x zlib/1.2.5
Host: localhost:9002
Accept: */*
Cookie: token=12345678
Content-Type:text/plain
Content-Length: 243360
Expect: 100-continue
HTTP/1.1 100 Continue
HTTP/1.1 200 OK
Content-Type: text/plain
Date: Sun, 04 Aug 2013 17:12:39 GMT
Connection: keep-alive
Transfer-Encoding: chunked
E il servizio risponde senza riecheggiando il corpo della richiesta:
Echo service:
Url:/
Headers:
{
"user-agent": "curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8x zlib/1.2.5",
"host": "localhost:9002",
"accept": "*/*",
"cookie": "token=12345678",
"content-type": "text/plain",
"content-length": "243360",
"expect": "100-continue"
}
... e ultimo ricciolo debug è
Body:
Connection #0 to host localhost left intact
Closing connection #0
Inoltre, quando sottolineo prova con grande corpo della richiesta, ottengo un errore EPIPE. Come posso evitare questo?
- EDIT: Grazie a tentativi ed errori, ho fatto in modo che tutto funzionasse, e continua a essere un problema di temporizzazione. Anche se è ancora strano, poiché il timeout fa sì che il carico utile venga restituito, ma la durata del timeout non viene presa in considerazione. In altre parole, se imposto il timeout a 5 secondi o 500 secondi, il carico utile viene correttamente reindirizzato alla richiesta e la connessione viene interrotta.
Ecco la modifica:
http.createServer(function (req, res) {
try {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('Echo service: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
res.write('\nBody:"\n');
req.pipe(res);
} catch(ex) {
console.log(ex);
// how to change response code to error here? since headers have already been written?
} finally {
setTimeout((function() {
res.end();
}), 500000);
}
}).listen(TARGET_SERVER.port);
?
Nota che verrà visualizzata una richiesta su 9002. Questo è un proxy inverso (semplice nodo-http-proxy su 8000, il target). Colpire direttamente il bersaglio produce gli stessi risultati. –