We're running into the same problem having our front-end devs setup their localhost laravel API. Please let us know if you find a solution. Thanks in Advance!
May 23, 2016
3
Level 1
Failed to connect to 127.0.0.1 port 8181: Connection refused
I am having a problem sending http requests to a Laravel API via a NodeJS server on my local machine.
I run my Laravel app on port 8181:
$ php artisan serve --port=8181
Laravel development server started on http://localhost:8181/
But when I attempt to send requests to this endpoint via a express.js server I get the below response:
$ npm run server-dev
> [email protected] 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' }
When attempting to hit localhost with a curl request, I get a successfull response:
$ 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"}
but if I switch out localhost for 127.0.0.1 it fails:
$ 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
In my node app, my base url is set to use localhost:
axios.defaults.baseURL = 'http://localhost:8181/api';
However, it seems that node translates this to 127.0.0.1. Is there a way to force node to use localhost?
EDIT:
I also tried adding the below to my hosts file:
127.0.0.1 localhost
Please or to participate in this conversation.