Please can anybody help me to find out how to get the server socket context in node.js, so that i will come to know request came on which port number on my server. I can read the server port if i request using http headers but I want it through network and something like socket context which tells request came on which port number.
Here is the sample code:
var http=require('http');
var url = require('url');
var ports = [7006, 7007, 7008, 7009];
var servers = [];
var s;
function reqHandler(req, res) {
var serPort=req.headers.host.split(":");
console.log("PORT:"+serPort[1]);//here i get it using http header.
}
ports.forEach(function(port) {
s = http.createServer(reqHandler);
s.listen(port);
servers.push(s);
});
Please can anybody help me to find out how to get the server socket context in node.js, so that i will come to know request came on which port number on my server. I can read the server port if i request using http headers but I want it through network and something like socket context which tells request came on which port number.
Here is the sample code:
var http=require('http');
var url = require('url');
var ports = [7006, 7007, 7008, 7009];
var servers = [];
var s;
function reqHandler(req, res) {
var serPort=req.headers.host.split(":");
console.log("PORT:"+serPort[1]);//here i get it using http header.
}
ports.forEach(function(port) {
s = http.createServer(reqHandler);
s.listen(port);
servers.push(s);
});
Share
Improve this question
edited Oct 10, 2013 at 13:15
Brad
163k55 gold badges377 silver badges552 bronze badges
asked Oct 10, 2013 at 13:14
Anuradha SinghAnuradha Singh
1671 gold badge1 silver badge7 bronze badges
4
- 2 Each HTTP server can only listen to one port at a time. Also, this is super easy to do with Express. expressjs.com/api.html#app.listen – Brad Commented Oct 10, 2013 at 13:18
- What do you mean by network/socket context ? without getting request on the server. – user568109 Commented Oct 10, 2013 at 16:58
- socket context means whole connection object on the particular listen socket.I just wanted to know on which of the above ports request came on the sever. – Anuradha Singh Commented Oct 11, 2013 at 4:05
- @Brad So uh... how to listen on two ports at once then with identical behavior on both? – Michael Commented Jul 22, 2022 at 18:50
1 Answer
Reset to default 11The req
object has a reference to the underlying node socket. You can easily get this information as documented at: http://nodejs.org/api/http.html#http_message_socket and http://nodejs.org/api/net.html#net_socket_remoteaddress
Here is your sample code modified to show the local and remote socket address information.
var http=require('http');
var ports = [7006, 7007, 7008, 7009];
var servers = [];
var s;
function reqHandler(req, res) {
console.log({
remoteAddress: req.socket.remoteAddress,
remotePort: req.socket.remotePort,
localAddress: req.socket.localAddress,
localPort: req.socket.localPort,
});
}
ports.forEach(function(port) {
s = http.createServer(reqHandler);
s.listen(port);
servers.push(s);
});