0
由於我已經有一臺服務器並在本地主機上運行(見進一步下跌的例子),在節點命令行同時在線,我得到如下:爲什麼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