0

So im trying to build 2 separate applications 1 that used as a backend (Laravel as a REST api) and Angular application as the client, eventually those 2 apps have to work together under the same domain as a single web app.

What im trying to accomplish:

My Angular app is a single page application that boot from index.html, all the routes are handled by Angular except /api/* that should be handled by Laravel app. Im using 2 different apps in order to build web app more dynamic so i can easily change my backend framework and technologies and testing each app as a 'stand-alone' more easily. I dont want to use CORS in my response headers because my REST API serves ONLY my Angular app and not other applications such as api for developers. I want to use proxy that will foward all requests come from http://localhost:9100/api/* to: http://localhost:9000/api/*

Firstly im running Laravel on port 9000 by running:

php artisan serve --port 9000

And Angular app under port 9100 by running a gulp task (index.html is in the path ./src):

gulp.task('webserver', function(){
 connect.server({
    root: './src',
    port: 9100,
    middleware: function(connect, o) {
        var url = require('url');
        var proxy = require('proxy-middleware');
        var options = url.parse('http://localhost:9000/api');
        options.route = '/api';
        return [proxy(options)];
    }
 });
});

Both apps work perfectly as a stand-alone, but when im trying to navigate to: http://localhost:9100/api/v1/comments i receive the following error:

Error: connect ECONNREFUSED at errnoException (net.js:904:11) at Object.afterConnect [as oncomplete] (net.js:895:19)

I tried to investigate the cause of this problem, some people say it connected to my hosts file so i had to add the line: 127.0.0.1 localhost

But it doesnt work. I tried different gulp task:

gulp.task('webserver', function() {
 gulp.src("./src")
    .pipe(webserver({
        port: 9100,
        livereload: true,
        open: 'http://localhost:9100',
        proxies: [
            {
                source: '/api', target: 'http://localhost:9000/api'
            }
        ]
    }));
});

And i receive the exact same error...

My develop environment is Windows 10 x64 bit.

Lihai
  • 323
  • 2
  • 6
  • 18
  • You said laravel is running on port 9000 why would it respond to: http://localhost:9100/api/v1/comments Or is that a url in your angular site also? – Pitchinnate Oct 06 '15 at 14:35
  • because I added a proxy, and when i send an http request to localhost:9100/api/v1/comments , theoretically i should get response from localhosy:9000/api/v1/comments, I do it because i need 2 different apps to run under the same domain thats the only way i know... My angular app is running on port 9100. – Lihai Oct 06 '15 at 14:49

1 Answers1

1

Did you try to use http-proxy-middleware instead of proxy-middleware?

I experienced the same error with proxy-middleware. (Gulp browser-sync - redirect API request via proxy)

Error: connect ECONNREFUSED
    at errnoException (net.js:904:11)
    at Object.afterConnect [as oncomplete] (net.js:895:19)

Ended up creating http-proxy-middleware, which solved the issue in my case.

proxy-middleware somehow didn't work on the corporate network. http-proxy just did. (http-proxy-middleware uses http-proxy to do actual proxying)

Guess you are using gulp-webserver; The proxy can be added like:

var proxyMiddleware = require('http-proxy-middleware');

gulp.task('webserver', function() {
 gulp.src("./src")
    .pipe(webserver({
        port: 9100,
        livereload: true,
        open: 'http://localhost:9100',
        middleware: [proxyMiddleware('/api', {target: 'http://localhost:9000'})]
    }));
});

Never found out why this error is thrown with proxy-middleware in the corporate network ...


Update:

Think this question has been answered:

It was a problem with the artisan server, had to run it this way:

php artisan serve --host 0.0.0.0

Source:

https://github.com/chimurai/http-proxy-middleware/issues/38

https://github.com/chimurai/http-proxy-middleware/issues/21#issuecomment-138132809

Community
  • 1
  • 1
chimurai
  • 1,285
  • 1
  • 16
  • 18
  • It doesnt work either, im receiving undifined error: [HPM] Proxy error: ECONNREFUSED. undefined -> "localhost:9000/api/v1/comments" In the console. And in the browser the following error displayed: "Error occured while trying to proxy to: localhost:9000/api/v1/comments". Full console output: [21:16:02] Starting 'webserver'... [HPM] Proxy created: /api/v1 -> http://localhost:9000/api/v1 [21:16:02] Webserver started at http://localhost:9100 [21:16:02] Finished 'webserver' after 9.33 ms [HPM] Proxy error: ECONNREFUSED. undefined -> "localhost:9000/api/v1/comments" – Lihai Oct 07 '15 at 18:19
  • I found out the cause according to this issue here: https://github.com/chimurai/http-proxy-middleware/issues/21 I had to run Laravel server with 'host' flag. However im receiving an error from Laravel: "NotFoundHttpException" when i navigate to: localhost:9100/api/v1/comments – Lihai Oct 07 '15 at 18:33
  • perhaps setting the `changeOrigin` to `true` might help `proxyMiddleware('/api', {target: 'http://localhost:9000', changeOrigin:true}` – chimurai Oct 07 '15 at 19:31
  • Still doesnt work, what does this error even means: [HPM] Proxy error: ECONNREFUSED. undefined -> "localhost:9000/api/v1/comments" I cant debug it... – Lihai Oct 07 '15 at 19:50
  • undefined, is probably cause by an older version of nodejs. It should log: [HPM] Proxy error: ECONNREFUSED. localhost:9100 -> "localhost:9000/api/v1/comments". It means it couldn't proxy the request to the targetUrl. Can you try to set the HOST http request header explicitly, as suggested here: https://github.com/nodejitsu/node-http-proxy/issues/619 `proxyMiddleware('/api', {target: 'http://localhost:9000', headers: {host:'localhost:9000'}}` – chimurai Oct 07 '15 at 20:10
  • It doesnt work.. :( i dont know how to resolve it. Any other suggestion how to make Laravel and Angular work under the same domain when i create them on different git repositories? – Lihai Oct 09 '15 at 04:15
  • When a proxy work out of the box its just like magic. But when it doesn't it is usually a configuration issue in either the proxy and/or server. Also the network configuration needs to be considered, i.e. firewalls, corporate proxy, etc... Boils down to verifying what works and try isolate the issue. Manually verifying the requests made to the server would be a good start. – chimurai Oct 09 '15 at 09:00
  • Thanks, running laravel on 0.0.0.0, instead of localhost, solved the problem. – Alex Shuraits Jun 28 '16 at 11:37
  • i have set larvel on both 0.0.0.0 and 12.0.0.1, but it not work for me, have deployed gulp on 3000, and laravel api on 8000, but api request still appending :3000 with api request – Muhammad Akber Khan Nov 30 '17 at 13:32