Discussion:
[nodejs] Getting the port number from the service name
Jean Marie
2018-03-21 09:50:05 UTC
Permalink
Hello

I use the method createServer from class http and then listen to a port as
follows :

var server = http.createServer();
server.listen(port, function () {
...
}

the problem is that I only have the service name
and listening to a service doesn't work :

var server = http.createServer();
server.listen(service, function () {
...
}


so I need to read and parse the file /etc/services to get the port
associated with the service

is there a simpler way to get the port from the service ?

thanks in advance

Jean-Marie
--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/cc49bb5a-73e3-410d-8ffc-4613c9e6c9bf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Mikkel Wilson
2018-03-28 18:44:43 UTC
Permalink
So, a 'service' is a logical human thing, not a fixed, networking thing.
The items in /etc/services are just a handy 'commonly used' mapping, but
aren't strictly or adhered to or used to configure the applications on your
host. You can listen to HTTP on port 1337 just as well as you can on port
80 (with proper permissions). You can get the ports that a *currently
running* process is using, but I don't think that helps you.

It seems you're trying to listen to a port and respond with HTTP traffic. A
common pattern to dynamically choose the port to listen to is with
environment variables.

var server = http.createServer();
server.listen(process.env.PORT, function () {
...
}

And then start the application by specifying the port on the command line:

$ PORT=1337 node server.js

This lets you change the port easily without modifying the code or run
multiple copies of your server listening to different ports at the same
time.

HTH,
Mikkel
Post by Jean Marie
Hello
I use the method createServer from class http and then listen to a port as
var server = http.createServer();
server.listen(port, function () {
...
}
the problem is that I only have the service name
var server = http.createServer();
server.listen(service, function () {
...
}
so I need to read and parse the file /etc/services to get the port
associated with the service
is there a simpler way to get the port from the service ?
thanks in advance
Jean-Marie
--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/b8e89af8-cc3e-4bb2-aa4f-cbd69648ce84%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...