johnpapa / lite-server

Lightweight node server
MIT License
2.32k stars 267 forks source link

proxy #61

Open gadieichhorn opened 8 years ago

gadieichhorn commented 8 years ago

its more of a question than an issue...

I am following the NG2 ToH pattern and need to add a proxy to my local server API but would like to keep it simple from the command prompt in the node script section of the package.json

any url calls to /server/api redirect to http://192.168.99.100/server/api

thanks

cgmartin commented 8 years ago

@gadieichhorn This should be possible by adding a proxy middleware in a custom bs-config.js, ie: http-proxy-middleware https://github.com/chimurai/http-proxy-middleware/blob/master/examples/browser-sync/index.js

Joebob12 commented 8 years ago

Has anyone been able to put in a bs-config file and get it working? I'm Actually getting in the terminal a "Did not detect a 'bs-config.json' or 'bs-config.js' overide file using lite-server defaults when I have it in my directory. See image of test project error here Not sure if i'm going crazy or not.

cgmartin commented 8 years ago

@gadieichhorn here is a working example with lite-server

Run:

$ npm install --save-dev http-proxy-middleware connect-history-api-fallback

File: bs-config.js

var proxyMiddleware = require('http-proxy-middleware');
var fallbackMiddleware = require('connect-history-api-fallback');

module.exports = {
    server: {
        middleware: {
            1: proxyMiddleware('/api', {
                target: 'http://www.example.org',
                changeOrigin: true   // for vhosted sites, changes host header to match to target's host
            }),

            2: fallbackMiddleware({
                index: '/index.html', verbose: true
            })
        }
    }
};
maximedupre commented 8 years ago

I think the history middleware should be before the proxy middleware. For some reason, when the proxy middleware is first, it never reaches the history one.

chimurai commented 8 years ago

Thanks @cgmartin for the example.

Added lite-server to the list of examples: https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/servers.md#lite-server

pabl-o-ce commented 8 years ago

thanks it works @cgmartin and @maximedupre! keep with the good work