无法连接到127.0.0.1端口8181:连接被拒绝

时间:2016-05-24 21:53:32

标签: javascript php node.js laravel curl

我在通过本地计算机上的NodeJS服务器向Laravel API发送http请求时遇到问题。

我在端口8181上运行我的Laravel应用程序:

$ php artisan serve --port=8181
Laravel development server started on http://localhost:8181/

但是当我尝试通过express.js服务器向此端点发送请求时,我得到以下响应:

$ npm run server-dev
> stryve@0.3.0-alpha server-dev F:\Code\stryve
> SET PROD_ENV=0 & node src/server.js


Listening on *:3333

User connected and ready.
A user has connected to the server.
{ [Error: connect ECONNREFUSED 127.0.0.1:8181]
  code: 'ECONNREFUSED',
  errno: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 8181 }
{ errorCode: 0,
  errorMessage: 'connect ECONNREFUSED 127.0.0.1:8181' }

尝试使用curl请求点击localhost时,我得到了成功的回复:

$ curl http://localhost:8181/
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    46    0    46    0     0    978      0 --:--:-- --:--:-- --:--:--   978{"code":200,"status":"OK","message":"success"}

但如果我为localhost切换127.0.0.1,则会失败:

$ curl http://127.0.0.1:8181
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (7) Failed to connect to 127.0.0.1 port 8181: Connection refused

在我的节点应用中,我的基本网址设置为使用localhost

axios.defaults.baseURL = 'http://localhost:8181/api';

但是,节点似乎将其转换为127.0.0.1。有没有办法强制节点使用localhost

修改

我还尝试将以下内容添加到我的hosts文件中:

127.0.0.1       localhost

我使用Windows 10(64位)和Git Bash来运行所有命令。

编辑#2:

已禁用Windows防火墙,但仍然收到相同的错误。

0 个答案:

没有答案
相关问题