为什么NodeJS请求()在飞行模式下的本地主机上失败,但不是127.0.0.1? (视窗10)
问题描述:
由于我已经有一台服务器并在本地主机上运行(见进一步下跌的例子),在节点命令行同时在线,我得到如下:为什么NodeJS请求()在飞行模式下的本地主机上失败,但不是127.0.0.1? (视窗10)
> var x = request('http://localhost:8080/test.html',
... function(err) { if (err) console.log(err) })
undefined
>
我期待一直得到上述结果。
如果我切换到飞行模式,我得到如下:
> var x = request('http://localhost:8080/test.html',
... function(err) { if (err) console.log(err) })
undefined
> { Error: getaddrinfo ENOENT localhost:8080
at Object.exports._errnoException (util.js:1022:11)
at errnoException (dns.js:33:15)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)
code: 'ENOENT',
errno: 'ENOENT',
syscall: 'getaddrinfo',
hostname: 'localhost',
host: 'localhost',
port: '8080' }
与127.0.0.1
代替localhost
再次尝试这个工程与否我在飞行模式下我。
问题
为什么本地主机不工作?我可以看到这与Windows的DNS解析器有关。
客户端代码设置上述
您需要安装require第一:
C:\> npm install require
更广阔的背景
我熬下来最简单的,我可以。有关更广泛的内容,请参阅Why does web-component-tester time out in flight mode?。
例服务器
的问题是不是下面的服务器而言,这只是一个简单的例子。我想可能任何本地服务器都会这样做,而不仅仅是一个NodeJS服务器。问题出在上面详述的客户端代码中。
设置了例如服务器:
C:\> npm install connect serve-static
server.js:
var connect = require('connect')
var serveStatic = require('serve-static')
connect().use(serveStatic(__dirname)).listen(8080)
的test.html:
<html>Really not important, but necessary for completeness</html>
启动服务器:
C:\> node server.js