confused about app.listen()

2971 views
Skip to first unread message

Lothar Pfeiler

unread,
Jan 26, 2012, 8:33:24 AM1/26/12
to Express
Hi,
I'm new to express. I really like the concept of express. It's cool.
Recently, I ran into a problem and my google search didn't really
help. So, I'm not sure if I use the function correctly.

I would like my app to listen on localhost. So, I tried

app.listen(3000, 'localhost'); //Gives me a TypeError: Cannot read
property 'port' of null
app.listen({ port: 3000, host: 'localhost' }); //now the app listens
to port 37405

I am using version 2.5.5 of express. By the way, just using nodes.js
createServer...listen(3000, "localhost") works as expected.

Could somebody shed light on this, please?

Lothar

Prashanth

unread,
Jan 26, 2012, 9:51:04 PM1/26/12
to expre...@googlegroups.com


You dont have to listen on specific host, just app.listen(3000) should
work.As long as your localhost resolves its should work.


--
regards,
Prashanth
twitter: munichlinux
irc: munichlinux, JSLint, munichpython.

Lothar Pfeiler

unread,
Jan 27, 2012, 1:07:06 AM1/27/12
to Express
Well, I just don't like it, that you can see port 3000 from outside.
People should only see one way to the server. My nginx webserver in
the front is also doing some authentication for some parts of the app.

I stumbled over the compatibility between express and nodes. I'm not
sure, but I think on my webserver the version of nodes is already
0.6.9 and express 2.5.5. And I didn't check the version of connect at
all. I'm going to look at it today.

By the way, I start the app with "forever". Is it somehow possible, to
have 2 versions of nodes on the server and tell forever to run express
apps with a certain node (Just in case, some of you use 'forever' and
deal with different node versions)?

Regards,
Lothar

On Jan 27, 3:51 am, Prashanth <munichli...@gmail.com> wrote:

TJ Holowaychuk

unread,
Jan 27, 2012, 1:06:25 AM1/27/12
to expre...@googlegroups.com
by default you're binding to INADDR_ANY, you can pass a host

-- 
TJ Holowaychuk
--
You received this message because you are subscribed to the Google Groups "Express" group.
To post to this group, send email to expre...@googlegroups.com.
To unsubscribe from this group, send email to express-js+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/express-js?hl=en.

Ryan Schmidt

unread,
Jan 27, 2012, 1:48:02 AM1/27/12
to expre...@googlegroups.com

On Jan 27, 2012, at 00:07, Lothar Pfeiler wrote:

> Is it somehow possible, to
> have 2 versions of nodes on the server and tell forever to run express
> apps with a certain node (Just in case, some of you use 'forever' and
> deal with different node versions)?

For this, I believe you will want to look into nvm.

https://github.com/creationix/nvm


Lothar Pfeiler

unread,
Jan 27, 2012, 7:42:27 AM1/27/12
to Express
Hi Ryan,

thanks for this advice. I installed nvm. It's really great. After
installing v0.4.9, app.listen(3000, 'localhost') worked as expected.

However, I ran into a new issue. "forever" requires a higher node
version. Any solution for that?

Regards,
Lothar

Ryan Schmidt

unread,
Jan 27, 2012, 7:54:50 AM1/27/12
to expre...@googlegroups.com

On Jan 27, 2012, at 06:42, Lothar Pfeiler wrote:

> thanks for this advice. I installed nvm. It's really great. After
> installing v0.4.9, app.listen(3000, 'localhost') worked as expected.
>
> However, I ran into a new issue. "forever" requires a higher node
> version. Any solution for that?

Then install a newer version of node :) or perhaps an older version of forever if you really need to. But if there's a problem with the current versions of the software, I'm sure the authors would want to hear about it so they can fix it.


Michael "Kato" Wulf

unread,
Jan 27, 2012, 10:35:54 AM1/27/12
to expre...@googlegroups.com
If your only goal is to keep 3000 from being open to the outside wall, isn't that a job that can be conquered in about 10 seconds with a firewall/iptables setting?

I don't see a need for nvm if you just want to close the port off to marauding cyber-bandits. : )

--
You received this message because you are subscribed to the Google Groups "Express" group.
To post to this group, send email to expre...@googlegroups.com.
To unsubscribe from this group, send email to express-js+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/express-js?hl=en.




--
Michael Wulf


Lothar Pfeiler

unread,
Jan 27, 2012, 11:25:13 AM1/27/12
to Express
Hi Michael,

of course, you can use a firewall. But it's a common thing to bind
your apps to the loopback device if you don't want it to communicate
with the outside world. And why should I first expose it and than
limit it again with a third application involved?

Just for the record, installing nvm is not the solution, but using the
supported node.js version is the key. nvm just makes your life easier
to manage several versions of nodes on the same machine.

Regards,
Lothar

On Jan 27, 4:35 pm, "Michael \"Kato\" Wulf" <katow...@gmail.com>
wrote:
> If your only goal is to keep 3000 from being open to the outside wall,
> isn't that a job that can be conquered in about 10 seconds with a
> firewall/iptables setting?
>
> I don't see a need for nvm if you just want to close the port off to
> marauding cyber-bandits. : )
>
> On Fri, Jan 27, 2012 at 5:54 AM, Ryan Schmidt <google-2...@ryandesign.com>wrote:
>
>
>
>
>
>
>
>
>
>
>
> > On Jan 27, 2012, at 06:42, Lothar Pfeiler wrote:
>
> > > thanks for this advice. I installed nvm. It's really great. After
> > > installing v0.4.9, app.listen(3000, 'localhost') worked as expected.
>
> > > However, I ran into a new issue. "forever" requires a higher node
> > > version. Any solution for that?
>
> > Then install a newer version of node :) or perhaps an older version of
> > forever if you really need to. But if there's a problem with the current
> > versions of the software, I'm sure the authors would want to hear about it
> > so they can fix  it.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Express" group.
> > To post to this group, send email to expre...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > express-js+...@googlegroups.com.
> > For more options, visit this group at
> >http://groups.google.com/group/express-js?hl=en.
>
> --
> Michael Wulf
> 775-235-8398
> web site <http://www.michaelwulf.com> |
> contact<http://www.michaelwulf.com/blog/chat-with-wulf>

Michael "Kato" Wulf

unread,
Jan 27, 2012, 11:31:24 AM1/27/12
to expre...@googlegroups.com
I use nvm as well; terribly convenient : )

As for "expose and limit again", I guess I'd just like to make the point that the firewall should already have the port closed. The only ports opened to the outside world should be ones explicitly opened by the admin. If that's not the case, it's time to read a quick tutorial on firewalls.

That said, I certainly don't object to setting up apps to run on a local feedback loop, but effort vs reward (in this specific circumstance where things aren't going well) ought to be next to null considering the firewall should already be blocking the port.

Alright, I'm done being practical now. Back to your regularly scheduled ideal designs : )

Lothar Pfeiler

unread,
Jan 27, 2012, 1:58:51 PM1/27/12
to Express
Yeah Micheal,

you got a point there. And I don't mind thinking practical. Sometimes
I'm just in this "inch pincher" mood, where I first want to use a new
framework the right way before thinking about workarounds. :)

Lothar

On Jan 27, 5:31 pm, "Michael \"Kato\" Wulf" <katow...@gmail.com>
wrote:
> I use nvm as well; terribly convenient : )
>
> As for "expose and limit again", I guess I'd just like to make the point
> that the firewall should already have the port closed. The only ports
> opened to the outside world should be ones explicitly opened by the admin.
> If that's not the case, it's time to read a quick tutorial on firewalls.
>
> That said, I certainly don't object to setting up apps to run on a local
> feedback loop, but effort vs reward (in this specific circumstance where
> things aren't going well) ought to be next to null considering the firewall
> should already be blocking the port.
>
> Alright, I'm done being practical now. Back to your regularly scheduled
> ideal designs : )
>

Ryan Schmidt

unread,
Jan 28, 2012, 8:01:19 AM1/28/12
to expre...@googlegroups.com

On Jan 27, 2012, at 10:25, Lothar Pfeiler wrote:

> But it's a common thing to bind
> your apps to the loopback device if you don't want it to communicate
> with the outside world.

That sounds good. How do I do that?


Lothar Pfeiler

unread,
Jan 28, 2012, 8:25:35 AM1/28/12
to Express
app.listen(3000, 'localhost');

You can provide certain IP addresses or hostnames to let an app only
listen on those IP addresses. The default is to listen to 0.0.0.0,
which means I take any request from anywhere for port 3000. Check
"netstat -ap|grep node" and have a look. The default is that your app
listens to *:3000. After providing localhost as hostname in
app.listen() you should see something like localhost.localdom:3000

But be aware that you use express 2.5.5 with a 0.4.* version of nodejs
as figured out now. :)

Regards,
Lothar

Ryan Schmidt

unread,
Jan 28, 2012, 8:59:02 AM1/28/12
to expre...@googlegroups.com
On Jan 28, 2012, at 07:25, Lothar Pfeiler wrote:

> app.listen(3000, 'localhost');
>
> You can provide certain IP addresses or hostnames to let an app only
> listen on those IP addresses. The default is to listen to 0.0.0.0,
> which means I take any request from anywhere for port 3000. Check
> "netstat -ap|grep node" and have a look. The default is that your app
> listens to *:3000. After providing localhost as hostname in
> app.listen() you should see something like localhost.localdom:3000
>
> But be aware that you use express 2.5.5 with a 0.4.* version of nodejs
> as figured out now. :)

I've moved on to node 0.6.x. Does anybody know if this is a bug in 0.6.x, or if there's a different way to do this in 0.6.x, or whether this feature has been removed?


Lothar Pfeiler

unread,
Jan 28, 2012, 10:31:18 AM1/28/12
to Express
The feature didn't move, because using createServer.listen(3000,
'localhost') works in nodes.js 0.6.x as expected. I guess, that
somehow the interface between express/connect and nodes.js just
changed. It might be a bug, but the express requirements of the github
site mention to use 0.4.x with 2.5.5. If it isn't working in express
3.x, then it's a bug, I would say.

vision media [ Tj Holowaychuk ]

unread,
Jan 28, 2012, 11:53:40 AM1/28/12
to expre...@googlegroups.com
In 3x the app itself is a Function, so you do:

http.createServer(app).listen(...)

however app#listen() for convenience is defined as:

app.listen = function(){
  var server = http.createServer(this);
  return server.listen.apply(server, arguments);
};

--
You received this message because you are subscribed to the Google Groups "Express" group.
To post to this group, send email to expre...@googlegroups.com.
To unsubscribe from this group, send email to express-js+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/express-js?hl=en.




--
Tj Holowaychuk
Vision Media
President & Creative Lead

Lothar Pfeiler

unread,
Feb 9, 2012, 8:06:51 AM2/9/12
to Express
Now, I would say it's a bug.

After visting the github page of express I realized that node 0.6.x is
supported by express 2.x.

So, as node 0.6.x is supported and app.listen(3000, 'localhost')
creates an error, I'd say this is a show stopping bug. I can work with
node v0.4.12, but on the other hand, doing a benchmark with ab gives
me errors I don't have with node v0.6.8. But that's another story
(hopefully).

Regards,
Lothar

On Jan 28, 5:53 pm, "vision media [ Tj Holowaychuk ]" <t...@vision-
media.ca> wrote:
> In 3x the app itself is a Function, so you do:
>
> http.createServer(app).listen(...)
>
> however app#listen() for convenience is defined as:
>
> app.listen = function(){
>   var server = http.createServer(this);
>   return server.listen.apply(server, arguments);
>
> };
>

tjholowaychuk

unread,
Feb 9, 2012, 11:44:16 AM2/9/12
to Express
@lothar I don't get an error with 2.x and 0.6.x

Lothar Pfeiler

unread,
Feb 11, 2012, 2:33:13 AM2/11/12
to Express
Hi TJ,

I found out that app.listen is actually working. I got confused,
because the line underneath it leads to the crash. app.address().port
When you create the app with the express binary, underneath the
app.listen command, you create a console message with the port. As
soon as you use a hostname or IP address in listen() the
console.log() function tears it down.

app.address() returns null

So, as I can't see that you're overriding server.address() somewhere,
it's a node bug.

Lothar

Lothar Pfeiler

unread,
Feb 11, 2012, 2:43:19 PM2/11/12
to Express
This is what Ben Noordhuis answered on the nodes group:

"It's a documentation bug.
`server.address()` isn't reliable until the 'listening' event is
emitted because Node may need to do a DNS lookup when you pass in a
host name, the results of which aren't immediately available.
I've updated the documentation in d3f6b09. "

Regards,
Lothar
Reply all
Reply to author
Forward
0 new messages