Using gulp-webserver with proxy and middleware

I am trying to get gulp-webserver to work with proxy and middleware.

My angular application needs to run gulp-webserver on port 3000 My Laravel backend is running on apache with URL: " http: //backend.api/api/v1 "

Now I would like to redirect / proxy all requests in my angular app which starts from '/ api' to 'backend.api / api /'

Example: url in angular for ajax: '/ api / v1 / users' should go to 'backend.api / api / v1 / users' and get json data from it.

sry for missing http before backend.api url - but stackoverflow won't let me have more than 2 links in one post.

I tried for hours to get it to work with gulp-webserver In gulp-connect it worked with this setting:

gulp.task('webserver', function () {
    plugins.connect.server({
        root: paths.distDevelopFolder,
        port: 3000,
        middleware: function (connect, o) {
            return [(function () {
                var url = require('url');
                var proxy = require('proxy-middleware');
                var options = url.parse('http://backend.api/api');
                options.route = '/api';
                return proxy(options);
            })(), historyApiFallback];
        }
    });
});

      

+3


source to share


2 answers


I am using gulp-webserver like this:



var webserver = require("gulp-webserver")    
gulp.task('webserver', function(){
    return gulp
        .src([paths.distDevelopFolder])
        .pipe(webserver({
            port: 3000,
            livereload: true,
            open: 'http://localhost:3000',
            proxies: [
                {
                    source: '/api', target: 'http://backend.api/api'
                }
            ]
        }));
    });

      

+5


source


You can try this generator:

https://www.npmjs.com/package/generator-angular-proxy



It has exactly what you need (specify some urls to proxy to the remote server) and poke fun at the rest.

0


source







All Articles