简体   繁体   English

如何在本地访问 Node.JS 项目?

[英]How to access Node.JS project locally?

I have a node.js project running on the ubuntu server.---> App is listening on 5000.我有一个 node.js 项目在 ubuntu 服务器上运行。---> App 正在监听 5000。

So, I want to access it locally.所以,我想在本地访问它。 Like http://server_ip:5000像 http://server_ip:5000

How can I do this?我怎样才能做到这一点?

First, you need to know the IP of your server.首先,您需要知道服务器的IP。 You can do it by using an ifconfig (look for interfaces like ethX , enpXsY , enoX where X and Y are numbers) or curl icanhazip.com command.您可以通过使用ifconfig (查找ethXenpXsYenoX类的接口,其中 X 和 Y 是数字)或curl icanhazip.com命令来完成此操作。 I would recommend to execute both of them and see if the IPs in the output match.我建议同时执行它们并查看输出中的 IP 是否匹配。 If they don't - your server might be located behind NAT and you won't be able to access it unless you forward ports on your NAT host which is outside of the scope of this question.如果没有 - 您的服务器可能位于 NAT 后面,除非您转发 NAT 主机上的端口,否则您将无法访问它,这超出了此问题的范围。

Another thing you need to keep in mind is that servers can be protected by firewalls.您需要记住的另一件事是服务器可以受到防火墙的保护。 The procedure of adding port 5000 to the allowlist varies for every firewall.将端口 5000 添加到许可名单的过程因防火墙而异。 sudo ufw allow 5000 can often do the trick on Ubuntu (but, once again, it may or may not work - for you this procedure can be different). sudo ufw allow 5000通常可以在 Ubuntu 上实现这一点(但是,再一次,它可能会也可能不会起作用 - 对你来说这个过程可能会有所不同)。

After this you should be able to access your server by typing http://<YOUR_SERVER_IP>:5000 in your browser address bar在此之后,您应该能够通过在浏览器地址栏中键入http://<YOUR_SERVER_IP>:5000来访问您的服务器

You need to get the IP of your computer (IPv4).您需要获取计算机的 IP (IPv4)。 Here's a link to a webpage that shows it to you https://www.whatismyip.com .这是一个指向网页的链接,该网页向您展示了https://www.whatismyip.com After this you can simply type in your IP and then add :<port> , so in your case :5000在此之后,您可以简单地输入您的 IP,然后添加:<port> ,因此在您的情况下为:5000

For access from the same machine use http://localhost:5000 or use hostname eg http://hostname:5000.要从同一台机器访问,请使用 http://localhost:5000 或使用主机名,例如 http://hostname:5000。 Get hostname by writing hostname command from the terminal.通过从终端编写主机名命令来获取主机

For access from other machines such as your mobile device or laptop, they all need to be connected to the same network, lets say all connected to the same WiFi network.要从其他机器(例如您的移动设备或笔记本电脑)进行访问,它们都需要连接到同一个网络,假设都连接到同一个 WiFi 网络。 In this case use the IP address of the machine that is running your server.在这种情况下,请使用运行服务器的机器的 IP 地址。 Lets say if your IP is 192.168.1.1 then for eg write this http://192.168.1.1:5000假设您的 IP 是 192.168.1.1 那么例如写这个http://192.168.1.1:5000

Use ifconfig for getting IP address.使用ifconfig获取 IP 地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM