DEV Community

Discussion on: Run your Node.js application on a headless Raspberry Pi

Collapse
 
chrisakers profile image
Chris Akers

Thanks for this, Bogdan. This was very easy to follow and clear. Although I ran into an issue that caused me to pull out my hair for several hours. I was following your example exactly. Everything was working well up to the point when I tried to make a request to the server from outside the Raspberry Pi. I went the ufw route and allowed port 3000. But my laptop on the same LAN could not get a response from the node server. (The same laptop was connected over SSH on port 22.) Running a port scan showed that only 22 was open. I tried many things including disabling/enabling ufw, rebooting, making ufw default allow, disabling ufw and setting iptables directly to the most permissive and simple set of allow rules, etc. Next on the hit list was the router. I went through all the router settings with a fine tooth comb. No help there.

I'm a web developer so I'm super comfortable with the node stuff, but the networking stuff is a bit out of my comfort zone. So I was sure that's where the problem was. There must be some firewall issue somewhere, right?

No. It turns out it was the node server all along. The example code passes the hostname to server.listen. So node is only responding to requests with that exact combination of hostname and port. Trying to hit it using a different hostname from outside the Pi failed every time. Once I changed to using the hostname-less listen method then it worked immediately and the port showed as open when running the port scan. The change:

server.listen(PORT, () => {
  console.log(`Server running on port ${PORT}.`);
});

It goes to show that sometimes the issue is where you least expect it. I hope this helps someone else if they run into this issue.

Collapse
 
bogdaaamn profile image
Bogdan Covrig

Great catch Chris! I spend some time taking a look to the documentation, see if I missed the parameters combination, but it seems indeed that it takes the exact combination of hostname and port.

Interesting to take a look at, I expected if you use the reverse proxy to get trough 127.0.0.1, right?

Collapse
 
chrisakers profile image
Chris Akers

Yes, I imagine that the reverse proxy would have worked properly since the hostname would match in that scenario. But I followed the "0. Don't use a Reverse Proxy :(" section since I was only making something for use in my LAN. The assertion at the end of that section is "Now you can access from the outside of the world! You can type your device's address followed by the port in your browser." This is not true since using the device's address will not match the hostname. :(

Thread Thread
 
bogdaaamn profile image
Bogdan Covrig

That's right, yeah. Thank you for the heads up, I've edited the code to a more general case