Created
November 11, 2017 03:38
-
-
Save mygoare/0c30acee00d58e06f01d5fd6ea131288 to your computer and use it in GitHub Desktop.
http-proxy-middleware use with browser-sync gulp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Module dependencies. | |
*/ | |
var browserSync = require('browser-sync').create() | |
var proxy = require('http-proxy-middleware') // require('http-proxy-middleware'); | |
/** | |
* Configure proxy middleware | |
*/ | |
var jsonPlaceholderProxy = proxy('/api', { | |
target: 'http://jsonplaceholder.typicode.com', | |
changeOrigin: true, // for vhosted sites, changes host header to match to target's host | |
logLevel: 'debug', | |
pathRewrite: { | |
'/api': '/' // 这样访问 /api 会被代理到 / | |
} | |
}) | |
/** | |
* Add the proxy to browser-sync | |
*/ | |
browserSync.init({ | |
server: { | |
baseDir: './', | |
port: 3000, | |
middleware: [jsonPlaceholderProxy] | |
} | |
}) | |
console.log('[DEMO] Server: listening on port 3000') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment