本文介绍了为什么NodeJS request()在飞行模式下在本地主机上失败,而不在127.0.0.1上失败?(Windows 10)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于我已经在本地主机上启动并运行了服务器(示例,请参见下一节),在线时在节点命令行 上,我得到以下信息:

Given I already have a server up and running on localhost (see further down for an example), at a node command line while online, I get the following:

> var x = request('http://localhost:8080/test.html',
...               function(err) { if (err) console.log(err) })
undefined
> 

我希望一直都能得到上述结果.

I expect to get the above result all the time.

如果我已切换到飞行模式,则会得到以下信息:

If I've switched to flight mode I get the following:

> 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 再次尝试此操作是否处于我处于飞行模式.

Trying this again with 127.0.0.1 instead of localhost works whether or not I'm in flight mode.

问题

为什么本地主机不起作用?我可以看到这与Windows的DNS解析器有关.

Why does localhost not work? I can see it's something to do with Windows's DNS resolver.

设置上面的客户端代码

您需要先安装要求:

C:\> npm install require

更广泛的环境

我将其简化为最简单的方法.请参阅>为什么web-component-tester在飞行中会超时模式?以了解更广泛的背景.

I've boiled this down to the simplest I can. See Why does web-component-tester time out in flight mode? for the broader context.

示例服务器

问题不是专门针对以下服务器,它只是一个简单的示例.我认为可能任何本地服务器都可以,而不仅仅是NodeJS.问题出在上面详述的客户端代码中.

The question isn't about the following server specifically, it's just a quick example. I think probably any local server would do, not just a NodeJS one. The problem is in the client code as detailed above.

示例服务器的设置:

C:\> npm install connect serve-static

server.js:

server.js:

var connect = require('connect')
var serveStatic = require('serve-static')
connect().use(serveStatic(__dirname)).listen(8080)

test.html:

test.html:

<html>Really not important, but necessary for completeness</html>

启动服务器:

C:\> node server.js

推荐答案

解决方案是安装Microsoft回送适配器,如下所述:

The solution is to install the Microsoft Loopback Adapter, as detailed below:

这篇关于为什么NodeJS request()在飞行模式下在本地主机上失败,而不在127.0.0.1上失败?(Windows 10)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 11:40