_All_`http-proxy` [options](https://github.com/nodejitsu/node-http-proxy#options) can be used, along with some extra `http-proxy-middleware`[options](#options).
:bulb:**Tip:**Settheoption`changeOrigin` to `true`for[name-basedvirtualhostedsites](http://en.wikipedia.org/wiki/Virtual_hosting#Name-based).
changeOrigin:true,// needed for virtual hosted sites
ws:true,// proxy websockets
pathRewrite:{
'^/api/old-path':'/api/new-path',// rewrite path
'^/api/remove/path':'/path',// remove base path
},
router:{
// when request.headers.host == 'dev.localhost:3000',
// override target 'http://www.example.org' to 'http://localhost:8000'
'dev.localhost:3000':'http://localhost:8000',
},
};
// create the proxy (without context)
constexampleProxy=createProxyMiddleware(options);
// mount `exampleProxy` in web server
constapp=express();
app.use('/api',exampleProxy);
app.listen(3000);
```
##Contextmatching
Providinganalternativewaytodecidewhichrequestsshouldbeproxied;Incaseyouarenotabletousetheserver's[`path`parameter](http://expressjs.com/en/4x/api.html#app.use) to mount the proxy or when you need more flexibility.
[RFC3986`path`](https://tools.ietf.org/html/rfc3986#section-3.3) is used for context matching.
Forfine-grainedcontrolyoucanusewildcardmatching.Globpatternmatchingisdoneby_micromatch_.Visit[micromatch](https://www.npmjs.com/package/micromatch) or [glob](https://www.npmjs.com/package/glob) for more globbing examples.
-**option.agent**:objecttobepassedtohttp(s).request(seeNode's[httpsagent](http://nodejs.org/api/https.html#https_class_https_agent) and [http agent](http://nodejs.org/api/http.html#http_class_http_agent) objects)
-String:newpath,forexample`cookiePathRewrite: "/newPath/"`. To remove the path, use `cookiePathRewrite: ""`. To set path to root use `cookiePathRewrite: "/"`.
Usetheshorthandsyntaxwhenverboseconfigurationisnotneeded.The`context` and `option.target`willbeautomaticallyconfiguredwhenshorthandisused.Optionscanstillbeusedifneeded.
InthepreviousWebSocketexamples,http-proxy-middlewarereliesonainitialhttprequestinordertolistentothehttp`upgrade` event. If you need to proxy WebSockets without the initial http request, you can subscribe to the server's http `upgrade`eventmanually.
server.on('upgrade',wsProxy.upgrade);// <-- subscribe to http 'upgrade'
```
##Interceptandmanipulaterequests
Interceptrequestsfromdownstreambydefining`onProxyReq` in `createProxyMiddleware`.
Currentlytheonlypre-providedrequestinterceptoris`fixRequestBody`, which is used to fix proxied POST requests when `bodyParser`isappliedbeforethismiddleware.
Interceptresponsesfromupstreamwith`responseInterceptor`. (Make sure to set `selfHandleResponse: true`)
Responseswhicharecompressedwith`brotli`, `gzip` and `deflate` will be decompressed automatically. The response will be returned as `buffer`([docs](https://nodejs.org/api/buffer.html)) which you can manipulate.