http-proxy-middleware icon indicating copy to clipboard operation
http-proxy-middleware copied to clipboard

Proxy error: ECONNREFUSED (Laravel)

Open lihai-benhaim opened this issue 11 years ago • 6 comments

Im trying to foward all requests from my Express app that runs on port 4365 to another application that runs on port 9000. In my case im trying to build a Laravel app that will be used only for api porpuses and angular app that will use that api.

This is the code i use:

var app             = require('express')()
    , proxyMiddleware = require('http-proxy-middleware')
    , http          = require('http').Server(app);
var context = '/api'; 
var options = {
    target: 'http://localhost:9000', // target host
    changeOrigin: true,               // needed for virtual hosted sites
    ws: true,                         // proxy websockets
};
app.use(proxyMiddleware(context, options));
http.listen(4365, "localhost", function(){
    console.log('Express server is listening on Port 4365 ');
});

Assume i have Laravel app running on port 9000 with api routing valid. When i run it and open the browser and type in the url: http:\localhost:4365\api\v1\comments i assume to get the json, but it shows me an error: "Error occured while trying to proxy to: localhost:4365/api/v1/comments"

console output: [HPM] Proxy created: /api -> http://localhost:9000 Express server is listening on Port 4365 [HPM] Proxy error: ECONNREFUSED. localhost -> "localhost:9000/api/v1/comments"

lihai-benhaim avatar Nov 13 '15 15:11 lihai-benhaim

It sounds like issue #21.

(don't see anything weird in your configuration.)

chimurai avatar Nov 13 '15 16:11 chimurai

Wow thank you so much that actually worked! Though why do we set host 0.0.0.0 and not 127.0.0.1? Also this way how do i debug my api? i always start it up though PhpStorm...

lihai-benhaim avatar Nov 13 '15 17:11 lihai-benhaim

Np. Just remembered someone else bump into a similar issue. No idea what the difference is between to two configurations.... I'm totally unfamiliar with Laravel.

But please do update this thread if you found the answer.

chimurai avatar Nov 13 '15 19:11 chimurai

Not sure what the difference is either, but the following also worked for me:

php artisan serve --host 127.0.0.1

awdyer avatar Apr 14 '16 22:04 awdyer

Thanks that's help !

mehdilamaaffar avatar Nov 04 '16 20:11 mehdilamaaffar

php artisan serve --host 127.0.0.1 thanks @awdyer

stanNo1 avatar Nov 10 '16 06:11 stanNo1