Reputation: 4490
I have a Node.js http
server that occasionally acts as a proxy for some server side code written in a different language.
So sometimes (but not always) http
requests are to be passed through to an server application via sockets. The responses coming from the application already contain the http headers.
The problem is that I would like to simply write the application response into the http response stream without worrying about writing the headers and content separately.
I could implement the entire http server using net sockets but I would like to eventually implement a node http framework as the front-end.
Using the http
module, is there a way to write directly to the underlying response socket?
Upvotes: 3
Views: 1909
Reputation: 163593
When a request comes in, you should be able to access the connection
property of the request.
var http = require('http');
http.createServer(function (req, res) {
req.connection.write(/* your data here */);
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
Note that if you do this, you are also responsible for closing the connection when done. You could also just pipe the two streams together.
Upvotes: 3