[英]Node.js seems to ignore my IP address i give, instead it uses localhost to open the server
I tried to solve it by myself, but i failed. 我试图自己解决它,但失败了。 At first i was just trying to create the simplest localhost server i could, and it worked, so i thought about online server (by port forwarding), for my friends to check my code out.
起初,我只是试图创建最简单的本地主机服务器,并且它可以正常工作,所以我想到了在线服务器(通过端口转发),以便我的朋友们可以检查我的代码。 I guess there is something i did competely wrong, so i'm asking you.
我猜我在做某件事时错了,所以我问你。 The code:
编码:
var http = require('http');
var options = {
hostname: '178.217.195.9',
port: 45555
};
var server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end("working?");
});
server.listen(options, () => {
console.log(`Server running at 178.217.195.9:45555`);
});`
Source: https://nodejs.org/docs/v8.11.2/api/synopsis.html 来源: https : //nodejs.org/docs/v8.11.2/api/synopsis.html
I was trying to run this code on the latest version (10.3.0), and today i tried with recommended one (8.11.2). 我试图在最新版本(10.3.0)上运行此代码,今天我尝试使用推荐版本(8.11.2)。 No changes, so i guess it has to be my fault.
没有变化,所以我想这一定是我的错。
What's happening? 发生了什么? When i check (after port forwarding) the 178.217.195.9:45555, it doesn't exist.
当我检查(端口转发后)178.217.195.9:45555时,它不存在。
But when i check localhost:45555, it works. 但是当我检查localhost:45555时,它可以工作。 I don't know what to say about it.
我不知道该说些什么。
It's weird, as the topic says, it seems to ignore my IP i typed, changing it with localhost. 正如主题所言,这很奇怪,它似乎忽略了我键入的IP,而是使用localhost对其进行了更改。
Maybe the code is wrong. 也许代码是错误的。 I've heard "just change 127.0.0.1 with your public IP and should work", so i did it.
我听说过“只要用您的公共IP更改127.0.0.1,就可以使用”,所以我做到了。
Is this code only for localhost servers? 该代码仅适用于本地主机服务器吗?
I don't know if it will help in any way, but normally i'm using p5.js library (The Coding Train), port forwarding is set as: Service Port - 45555, IP Address - 192.168.0.103 (checked in cmd), Internal Port - 45555, Protocol - TCP or UDP. 我不知道它是否有任何帮助,但是通常我使用的是p5.js库(编码火车),端口转发设置为: 服务端口 -45555, IP地址 -192.168.0.103(在cmd中检查) ), 内部端口 -45555, 协议 -TCP或UDP。
I added exception to my firewall for 45555 port. 我为45555端口的防火墙添加了例外。 Sometimes, when i try to change my code, to test other options, but the concept is the same, it says Error: listen EADDRNOTAVAIL 178.217.195.9:45555 .
有时,当我尝试更改代码以测试其他选项时,但是概念是相同的,它显示错误:侦听EADDRNOTAVAIL 178.217.195.9:45555 。
Changing the port doesnt help. 更改端口没有帮助。 Then im usually back to ^ this code.
然后,我通常会回到^此代码。 Through control panel i made my IP static.
通过控制面板,我将IP设为静态。 Ip adress: 192.168.0.103, Default Gateway: 192.168.0.1, Subnet Mask: 255.255.255.0 (if it will help in any way).
IP地址:192.168.0.103,默认网关:192.168.0.1,子网掩码:255.255.255.0(如果有任何帮助)。
Previously (i don't remember when exactly but more than 3 months ago) i also set prefered DNS servers as: 8.8.8.8 and 8.8.4.4. 以前(我不记得确切的时间,但已超过三个月前),我还将首选的DNS服务器设置为:8.8.8.8和8.8.4.4。 Current Node.js version is 8.11.2, npm 6.1.0.
当前的Node.js版本是8.11.2,npm 6.1.0。 I don't really want to use Heroku or other programs like that.
我真的不想使用Heroku或类似的其他程序。 I've heard something about WebSockets, was messing a little around it but nothing really happened.
我听说过一些有关WebSockets的信息,有些混乱,但实际上没有任何反应。 I don't any other ideas about useful informations there.
我对那里有用的信息没有其他想法。 I hope somebody will help me, i'll answer every question about other informations.
希望有人能帮助我,我会回答有关其他信息的每个问题。
In the options has to be host
and not hostname
. 在选项中必须是
host
而不是hostname
。 So for your code host
is not present in the options and as of that localhost
is used as default. 因此,对于您的代码
host
,这些选项中不存在该localhost
,并且默认情况下使用localhost
。
server.listen(options[, callback]) server.listen(options [,callback])
server.listen(options[, callback]) # Added in: v0.11.14 options <Object> Required. Supports the following properties: port <number> host <string> path <string> Will be ignored if port is specified. See Identifying paths for IPC connections. backlog <number> Common parameter of server.listen() functions. exclusive <boolean> Default: false readableAll <boolean> For IPC servers makes the pipe readable for all users. Default: false writableAll <boolean> For IPC servers makes the pipe writable for all users. Default: false callback <Function> Common parameter of server.listen() functions. Returns: <net.Server>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.