Skip to content

Instantly share code, notes, and snippets.

@mygoare
Created November 11, 2017 03:38
Show Gist options
  • Save mygoare/0c30acee00d58e06f01d5fd6ea131288 to your computer and use it in GitHub Desktop.
Save mygoare/0c30acee00d58e06f01d5fd6ea131288 to your computer and use it in GitHub Desktop.
http-proxy-middleware use with browser-sync gulp
/**
* 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