Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nicklaw5's avatar

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
0 likes
3 replies
tormiller's avatar

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!

fideloper's avatar

It appears that localhost is not using the same network as 127.0.0.1.

What I'm seeing on my MacOS (Sierra) is that the serve command binds to localhost by default.

What's happening is that the hostname localhost is attempting to use the ipv6 network rather than ipv4 network - and so 127.0.0.1 won't work.

For example:

php artisan serve --port=8181

# fails
curl 127.0.0.1

# works (quotes and brackets needed for ipv6 address)
curl "http://[::1]:8181"

# works, of course, as localhost is using the ipv6 network
curl localhost

The easiest solution is to define the host (network) for the serve command to listen on. Using 0.0.0.0 will listen on all ipv4 networks:

php artisan serve --host=0.0.0.0 --port=8181
1 like
candacetynes's avatar

Follow guides, you can check the Node.js server code: Review the code in your Node.js server (src/server.js) and confirm that it is making the HTTP requests to the correct URL (http: //localhost:8181/ api). Ensure that the Axios requests are being sent to the expected endpoint.

Please or to participate in this conversation.