我有一些创建套接字的网络代码,我想使用Node's standard HTTP API对它们进行HTTP。
从这样开始,
var http = require('http'),
net = require('net');
var port = 12345;
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end("Hello world");
}).listen(port);
var sock = net.createConnection({port: port});
如何将sock
传递给http.get()
或类似的?
答案 0 :(得分:1)
可以使用自定义http.Agent:
来完成此操作var agent = new http.Agent({ maxSockets: 1 });
agent.createSocket = function() {return sock};
http.get({
host: "localhost",
port: port,
path: "/",
agent: agent}, function(res) {
// ...
});