1

I'm creating a back end app with NodeJs. My app running at port 3001. It is accessible when i try to access it with localhost:3001 or 0.0.0.0:3001 or 127.0.0.1:3001, but when I try to access it with another device, it's mean I try to access with local ip (same network) 192.168.1.8, it is not working.

So, I try to open my port with ufw

sudo ufw allow 3001

and I check the ufw status with sudo ufw status verbose, this is the result is like below:

To                         Action      From
--                         ------      ----
2222                       ALLOW IN    Anywhere                  
3001/tcp                   ALLOW IN    192.168.1.8               
53                         ALLOW IN    Anywhere                  
22                         ALLOW IN    Anywhere                  
80/tcp                     ALLOW IN    Anywhere                  
443/tcp                    ALLOW IN    Anywhere                  
22/tcp                     ALLOW IN    Anywhere                  
192.168.1.8 3001/tcp       ALLOW IN    127.0.0.1                 
3001                       ALLOW IN    Anywhere                  
2222 (v6)                  ALLOW IN    Anywhere (v6)             
80/tcp (v6)                ALLOW IN    Anywhere (v6)             
443/tcp (v6)               ALLOW IN    Anywhere (v6)             
22/tcp (v6)                ALLOW IN    Anywhere (v6)             
3001 (v6)                  ALLOW IN    Anywhere (v6)  

but when I try to nmap 192.168.1.8. I saw the port not opened, here is the result from my nmap.

Starting Nmap 7.80 ( https://nmap.org ) at 2021-03-18 12:43 WIB
Nmap scan report for 192.168.1.8 (192.168.1.8)
Host is up (0.00014s latency).
All 1000 scanned ports on 192.168.1.8 (192.168.1.8) are closed

Nmap done: 1 IP address (1 host up) scanned in 0.06 seconds

but when I try to nmap localhost, the port 3001 has opened.

Starting Nmap 7.80 ( https://nmap.org ) at 2021-03-18 12:44 WIB
Nmap scan report for localhost (127.0.0.1)
Host is up (0.00014s latency).
Not shown: 998 closed ports
PORT     STATE SERVICE
631/tcp  open  ipp
3001/tcp open  nessus

Nmap done: 1 IP address (1 host up) scanned in 0.06 seconds

So, how to open my port to my local ip that accessible with another device in the same network? Thanks in advance.

6
  • Is nodeJS listening on all interfaces? What is the output of sudo lsof -i :3001 or sudo netstat -tunlp | grep ":3001" ? Mar 18 '21 at 8:04
  • ``` COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME node 15136 aequaliter 22u IPv4 173728 0t0 TCP localhost:3001 (LISTEN) ``` Mar 18 '21 at 9:07
  • that is the output of sudo lsof -i :3001 Mar 18 '21 at 9:07
  • 1
    That shows you are only listening on the loopback (localhost) interface. You need to configure nodejs to listen on all interfaces (I think via your nodejs server.listen() call). Mar 18 '21 at 9:15
  • how to do that? Mar 18 '21 at 9:18
1

Just as an example, node.js file:

const express = require('express');
const app = express();

const port=3000;

app.listen(port, () => console.log(`App listening on port ${port}`));

And then:

# lsof -i :3000
COMMAND    PID  USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
node    201089 wayne   21u  IPv6 2249996      0t0  TCP *:3000 (LISTEN)

*:3000 indicates port 3000 on all interfaces. Note that even though it says IPv6 it is actually listening IPv4 as well since we did not specify ipv6Only (nodejs server.listen doc).

3
  • Thanks, actually I'm using fastify. I'll try to find something on fastify docs that make it listen on all interfaces. Mar 18 '21 at 9:46
  • Thank you Mr. Wayne. It solved. Mar 18 '21 at 10:03
  • maybe for anyone using fastify that have the same problem with me. can open this link to solve it.... fastify.io/docs/v1.13.x/Server Mar 18 '21 at 10:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.