gonzalocasas / node-proxy-middleware

proxy as middleware
MIT License
146 stars 65 forks source link

Proper way to setup Gruntfile? #11

Closed meongx closed 11 years ago

meongx commented 11 years ago

I tried to setup grunt + connect to proxy request to /api into local apache for PHP back-end (i.e http://localhost:9000/api would be proxied to http://localhost:80). But when i requested http://localhost:9000/api it says Cannot GET /api

Below are excerpt from my Gruntfile (generated by yeoman and edited for my project). If required, i can post my full Gruntfile

First i define proxy and proxyOption at top of the file

var url = require('url');
var proxy = require('proxy-middleware');
var proxyOption = url.parse('http://localhost');
proxyOption.route = '/api';

var mountFolder = function (connect, dir) {
    return connect.static(require('path').resolve(dir));
};

Then add the middleware on initConfig

grunt.initConfig({
    connect: {
        options: {
            port: 9000,
            hostname: 'localhost'
        },
        livereload: {
            options: {
                middleware: function(connect) {
                    return [
                        mountFolder(connect, '.tmp'),
                        mountFolder(connect, 'app'),
                        proxy(proxyOption)
                    ]
                }
            }
        }
    }
})
andrewrk commented 11 years ago

Grunt has nothing to do with this project. See the readme and test/test.js for examples of how to use this code. Or just read the source; it's a whopping 80 lines of code.