\href{https://travis-ci.org/chimurai/http-proxy-middleware}{\texttt{ }} \href{https://coveralls.io/r/chimurai/http-proxy-middleware}{\texttt{ }} \href{https://david-dm.org/chimurai/http-proxy-middleware\#info=dependencies}{\texttt{ }} \href{https://snyk.io/test/npm/http-proxy-middleware}{\texttt{ }} \href{https://github.com/prettier/prettier}{\texttt{ }}

Node.\+js proxying made simple. Configure proxy middleware with ease for \href{https://github.com/senchalabs/connect}{\texttt{ connect}}, \href{https://github.com/strongloop/express}{\texttt{ express}}, \href{https://github.com/BrowserSync/browser-sync}{\texttt{ browser-\/sync}} and \href{\#compatible-servers}{\texttt{ many more}}.

Powered by the popular Nodejitsu \href{https://github.com/nodejitsu/node-http-proxy}{\texttt{ {\ttfamily http-\/proxy}}}. \href{https://github.com/nodejitsu/node-http-proxy}{\texttt{ }}\hypertarget{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12353}{}\doxysection{TL;\+DR}\label{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12353}
Proxy {\ttfamily /api} requests to {\ttfamily \href{http://www.example.org}{\texttt{ http\+://www.\+example.\+org}}}


\begin{DoxyCode}{0}
\DoxyCodeLine{var express = require('express')}
\DoxyCodeLine{var proxy = require('http-\/proxy-\/middleware')}
\DoxyCodeLine{}
\DoxyCodeLine{var app = express()}
\DoxyCodeLine{}
\DoxyCodeLine{app.use('/api', proxy(\{ target: 'http://www.example.org', changeOrigin: true \}))}
\DoxyCodeLine{app.listen(3000)}
\DoxyCodeLine{}
\DoxyCodeLine{// http://localhost:3000/api/foo/bar -\/> http://www.example.org/api/foo/bar }

\end{DoxyCode}


{\itshape All} {\ttfamily http-\/proxy} \href{https://github.com/nodejitsu/node-http-proxy\#options}{\texttt{ options}} can be used, along with some extra {\ttfamily http-\/proxy-\/middleware} \href{\#options}{\texttt{ options}}.

\+:bulb\+: {\bfseries{Tip\+:}} Set the option {\ttfamily change\+Origin} to {\ttfamily true} for \href{http://en.wikipedia.org/wiki/Virtual_hosting\#Name-based}{\texttt{ name-\/based virtual hosted sites}}.\hypertarget{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12354}{}\doxysection{Table of Contents}\label{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12354}

\begin{DoxyItemize}
\item \href{\#install}{\texttt{ Install}}
\item \href{\#core-concept}{\texttt{ Core concept}}
\item \href{\#example}{\texttt{ Example}}
\item \href{\#context-matching}{\texttt{ Context matching}}
\item \href{\#options}{\texttt{ Options}}
\begin{DoxyItemize}
\item \href{\#http-proxy-middleware-options}{\texttt{ http-\/proxy-\/middleware options}}
\item \href{\#http-proxy-events}{\texttt{ http-\/proxy events}}
\item \href{\#http-proxy-options}{\texttt{ http-\/proxy options}}
\end{DoxyItemize}
\item \href{\#shorthand}{\texttt{ Shorthand}}
\begin{DoxyItemize}
\item \href{\#appusepath-proxy}{\texttt{ app.\+use(path, proxy)}}
\end{DoxyItemize}
\item \href{\#websocket}{\texttt{ Web\+Socket}}
\begin{DoxyItemize}
\item \href{\#external-websocket-upgrade}{\texttt{ External Web\+Socket upgrade}}
\end{DoxyItemize}
\item \href{\#working-examples}{\texttt{ Working examples}}
\item \href{\#recipes}{\texttt{ Recipes}}
\item \href{\#compatible-servers}{\texttt{ Compatible servers}}
\item \href{\#tests}{\texttt{ Tests}}
\item \href{\#changelog}{\texttt{ Changelog}}
\item \href{\#license}{\texttt{ License}}
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12355}{}\doxysection{Install}\label{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12355}

\begin{DoxyCode}{0}
\DoxyCodeLine{\$ npm install -\/-\/save-\/dev http-\/proxy-\/middleware }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12356}{}\doxysection{Core concept}\label{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12356}
Proxy middleware configuration.\hypertarget{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12357}{}\doxysubsubsection{proxy(\mbox{[}context,\mbox{]} config)}\label{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12357}

\begin{DoxyCode}{0}
\DoxyCodeLine{var proxy = require('http-\/proxy-\/middleware')}
\DoxyCodeLine{}
\DoxyCodeLine{var apiProxy = proxy('/api', \{ target: 'http://www.example.org' \})}
\DoxyCodeLine{//                   \(\backslash\)\_\_\_\_/   \(\backslash\)\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_/}
\DoxyCodeLine{//                     |                    |}
\DoxyCodeLine{//                   context             options}
\DoxyCodeLine{}
\DoxyCodeLine{// 'apiProxy' is now ready to be used as middleware in a server. }

\end{DoxyCode}



\begin{DoxyItemize}
\item {\bfseries{context}}\+: Determine which requests should be proxied to the target host. (more on \href{\#context-matching}{\texttt{ context matching}})
\item {\bfseries{options.\+target}}\+: target host to proxy to. \+\_\+(protocol + host)\+\_\+
\end{DoxyItemize}

(full list of \href{\#options}{\texttt{ {\ttfamily http-\/proxy-\/middleware} configuration options}})\hypertarget{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12358}{}\doxysubsubsection{proxy(uri \mbox{[}, config\mbox{]})}\label{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12358}

\begin{DoxyCode}{0}
\DoxyCodeLine{// shorthand syntax for the example above:}
\DoxyCodeLine{var apiProxy = proxy('http://www.example.org/api') }

\end{DoxyCode}


More about the \href{\#shorthand}{\texttt{ shorthand configuration}}.\hypertarget{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12359}{}\doxysection{Example}\label{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12359}
An example with {\ttfamily express} server.


\begin{DoxyCode}{0}
\DoxyCodeLine{// include dependencies}
\DoxyCodeLine{var express = require('express')}
\DoxyCodeLine{var proxy = require('http-\/proxy-\/middleware')}
\DoxyCodeLine{}
\DoxyCodeLine{// proxy middleware options}
\DoxyCodeLine{var options = \{}
\DoxyCodeLine{  target: 'http://www.example.org', // target host}
\DoxyCodeLine{  changeOrigin: true, // needed for virtual hosted sites}
\DoxyCodeLine{  ws: true, // proxy websockets}
\DoxyCodeLine{  pathRewrite: \{}
\DoxyCodeLine{    '\string^/api/old-\/path': '/api/new-\/path', // rewrite path}
\DoxyCodeLine{    '\string^/api/remove/path': '/path' // remove base path}
\DoxyCodeLine{  \},}
\DoxyCodeLine{  router: \{}
\DoxyCodeLine{    // when request.headers.host == 'dev.localhost:3000',}
\DoxyCodeLine{    // override target 'http://www.example.org' to 'http://localhost:8000'}
\DoxyCodeLine{    'dev.localhost:3000': 'http://localhost:8000'}
\DoxyCodeLine{  \}}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{// create the proxy (without context)}
\DoxyCodeLine{var exampleProxy = proxy(options)}
\DoxyCodeLine{}
\DoxyCodeLine{// mount `exampleProxy` in web server}
\DoxyCodeLine{var app = express()}
\DoxyCodeLine{app.use('/api', exampleProxy)}
\DoxyCodeLine{app.listen(3000) }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12360}{}\doxysection{Context matching}\label{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12360}
Providing an alternative way to decide which requests should be proxied; In case you are not able to use the server\textquotesingle{}s \href{http://expressjs.com/en/4x/api.html\#app.use}{\texttt{ {\ttfamily path} parameter}} to mount the proxy or when you need more flexibility.

\href{https://tools.ietf.org/html/rfc3986\#section-3.3}{\texttt{ RFC 3986 {\ttfamily path}}} is used for context matching.


\begin{DoxyCode}{0}
\DoxyCodeLine{  foo://example.com:8042/over/there?name=ferret\#nose}
\DoxyCodeLine{  \(\backslash\)\_/   \(\backslash\)\_\_\_\_\_\_\_\_\_\_\_\_\_\_/\(\backslash\)\_\_\_\_\_\_\_\_\_/ \(\backslash\)\_\_\_\_\_\_\_\_\_/ \(\backslash\)\_\_/}
\DoxyCodeLine{   |           |            |            |        |}
\DoxyCodeLine{scheme     authority       path        query   fragment }

\end{DoxyCode}



\begin{DoxyItemize}
\item {\bfseries{path matching}}
\begin{DoxyItemize}
\item {\ttfamily proxy(\{...\})} -\/ matches any path, all requests will be proxied.
\item `proxy('/\textquotesingle{}, \{...\}){\ttfamily -\/ matches any path, all requests will be proxied. -\/}proxy(\textquotesingle{}/api\textquotesingle{}, \{...\}){\ttfamily -\/ matches paths starting with}/api\`{}
\end{DoxyItemize}
\item {\bfseries{multiple path matching}}
\begin{DoxyItemize}
\item `proxy(\mbox{[}'/api\textquotesingle{}, \textquotesingle{}/ajax\textquotesingle{}, \textquotesingle{}/someotherpath\textquotesingle{}\mbox{]}, \{...\})\`{}
\end{DoxyItemize}
\item {\bfseries{wildcard path matching}}

For fine-\/grained control you can use wildcard matching. Glob pattern matching is done by {\itshape micromatch}. Visit \href{https://www.npmjs.com/package/micromatch}{\texttt{ micromatch}} or \href{https://www.npmjs.com/package/glob}{\texttt{ glob}} for more globbing examples.
\begin{DoxyItemize}
\item `proxy('$\ast$$\ast$\textquotesingle{}, \{...\}){\ttfamily matches any path, all requests will be proxied. -\/}proxy(\textquotesingle{}$\ast$$\ast$/$\ast$.html\textquotesingle{}, \{...\}){\ttfamily matches any path which ends with}.html{\ttfamily  -\/}proxy(\textquotesingle{}/$\ast$.html\textquotesingle{}, \{...\}){\ttfamily matches paths directly under path-\/absolute -\/}proxy(\textquotesingle{}/api/$\ast$$\ast$/$\ast$.html\textquotesingle{}, \{...\}){\ttfamily matches requests ending with}.html{\ttfamily in the path of}/api{\ttfamily  -\/}proxy(\mbox{[}\textquotesingle{}/api/$\ast$$\ast$\textquotesingle{}, \textquotesingle{}/ajax/$\ast$$\ast$\textquotesingle{}\mbox{]}, \{...\}){\ttfamily combine multiple patterns -\/}proxy(\mbox{[}\textquotesingle{}/api/$\ast$$\ast$\textquotesingle{}, \textquotesingle{}!$\ast$$\ast$/bad.json\textquotesingle{}\mbox{]}, \{...\})\`{} exclusion
\end{DoxyItemize}

{\bfseries{Note}}\+: In multiple path matching, you cannot use string paths and wildcard paths together.
\item {\bfseries{custom matching}}

For full control you can provide a custom function to determine which requests should be proxied or not.
\end{DoxyItemize}


\begin{DoxyCode}{0}
\DoxyCodeLine{/**}
\DoxyCodeLine{ * @return \{Boolean\}}
\DoxyCodeLine{ */}
\DoxyCodeLine{var filter = function(pathname, req) \{}
\DoxyCodeLine{  return pathname.match('\string^/api') \&\& req.method === 'GET'}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{var apiProxy = proxy(filter, \{ target: 'http://www.example.org' \})}

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12361}{}\doxysection{Options}\label{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12361}
\hypertarget{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12362}{}\doxysubsection{http-\/proxy-\/middleware options}\label{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12362}

\begin{DoxyItemize}
\item {\bfseries{option.\+path\+Rewrite}}\+: object/function, rewrite target\textquotesingle{}s url path. Object-\/keys will be used as {\itshape Reg\+Exp} to match paths.
\end{DoxyItemize}


\begin{DoxyCode}{0}
\DoxyCodeLine{// rewrite path}
\DoxyCodeLine{pathRewrite: \{'\string^/old/api' : '/new/api'\}}
\DoxyCodeLine{}
\DoxyCodeLine{// remove path}
\DoxyCodeLine{pathRewrite: \{'\string^/remove/api' : ''\}}
\DoxyCodeLine{}
\DoxyCodeLine{// add base path}
\DoxyCodeLine{pathRewrite: \{'\string^/' : '/basepath/'\}}
\DoxyCodeLine{}
\DoxyCodeLine{// custom rewriting}
\DoxyCodeLine{pathRewrite: function (path, req) \{ return path.replace('/api', '/base/api') \}}

\end{DoxyCode}



\begin{DoxyItemize}
\item {\bfseries{option.\+router}}\+: object/function, re-\/target {\ttfamily option.\+target} for specific requests.
\end{DoxyItemize}


\begin{DoxyCode}{0}
\DoxyCodeLine{// Use `host` and/or `path` to match requests. First match will be used.}
\DoxyCodeLine{// The order of the configuration matters.}
\DoxyCodeLine{router: \{}
\DoxyCodeLine{    'integration.localhost:3000' : 'http://localhost:8001',  // host only}
\DoxyCodeLine{    'staging.localhost:3000'     : 'http://localhost:8002',  // host only}
\DoxyCodeLine{    'localhost:3000/api'         : 'http://localhost:8003',  // host + path}
\DoxyCodeLine{    '/rest'                      : 'http://localhost:8004'   // path only}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{// Custom router function}
\DoxyCodeLine{router: function(req) \{}
\DoxyCodeLine{    return 'http://localhost:8004';}
\DoxyCodeLine{\}}

\end{DoxyCode}



\begin{DoxyItemize}
\item {\bfseries{option.\+log\+Level}}\+: string, \mbox{[}\textquotesingle{}debug\textquotesingle{}, \textquotesingle{}info\textquotesingle{}, \textquotesingle{}warn\textquotesingle{}, \textquotesingle{}error\textquotesingle{}, \textquotesingle{}silent\textquotesingle{}\mbox{]}. Default\+: `\textquotesingle{}info'\`{}
\item {\bfseries{option.\+log\+Provider}}\+: function, modify or replace log provider. Default\+: {\ttfamily console}.
\end{DoxyItemize}


\begin{DoxyCode}{0}
\DoxyCodeLine{// simple replace}
\DoxyCodeLine{function logProvider(provider) \{}
\DoxyCodeLine{  // replace the default console log provider.}
\DoxyCodeLine{  return require('winston')}
\DoxyCodeLine{\}}

\end{DoxyCode}



\begin{DoxyCode}{0}
\DoxyCodeLine{// verbose replacement}
\DoxyCodeLine{function logProvider(provider) \{}
\DoxyCodeLine{  var logger = new (require('winston')).Logger()}
\DoxyCodeLine{}
\DoxyCodeLine{  var myCustomProvider = \{}
\DoxyCodeLine{    log: logger.log,}
\DoxyCodeLine{    debug: logger.debug,}
\DoxyCodeLine{    info: logger.info,}
\DoxyCodeLine{    warn: logger.warn,}
\DoxyCodeLine{    error: logger.error}
\DoxyCodeLine{  \}}
\DoxyCodeLine{  return myCustomProvider}
\DoxyCodeLine{\}}

\end{DoxyCode}



\begin{DoxyItemize}
\item (DEPRECATED) {\bfseries{option.\+proxy\+Host}}\+: Use {\ttfamily option.\+change\+Origin = true} instead.
\item (DEPRECATED) {\bfseries{option.\+proxy\+Table}}\+: Use {\ttfamily option.\+router} instead.
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12363}{}\doxysubsection{http-\/proxy events}\label{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12363}
Subscribe to \href{https://github.com/nodejitsu/node-http-proxy\#listening-for-proxy-events}{\texttt{ http-\/proxy events}}\+:


\begin{DoxyItemize}
\item {\bfseries{option.\+on\+Error}}\+: function, subscribe to http-\/proxy\textquotesingle{}s {\ttfamily error} event for custom error handling.
\end{DoxyItemize}


\begin{DoxyCode}{0}
\DoxyCodeLine{function onError(err, req, res) \{}
\DoxyCodeLine{  res.writeHead(500, \{}
\DoxyCodeLine{    'Content-\/Type': 'text/plain'}
\DoxyCodeLine{  \})}
\DoxyCodeLine{  res.end(}
\DoxyCodeLine{    'Something went wrong. And we are reporting a custom error message.'}
\DoxyCodeLine{  )}
\DoxyCodeLine{\}}

\end{DoxyCode}



\begin{DoxyItemize}
\item {\bfseries{option.\+on\+Proxy\+Res}}\+: function, subscribe to http-\/proxy\textquotesingle{}s {\ttfamily proxy\+Res} event.
\end{DoxyItemize}


\begin{DoxyCode}{0}
\DoxyCodeLine{function onProxyRes(proxyRes, req, res) \{}
\DoxyCodeLine{  proxyRes.headers['x-\/added'] = 'foobar' // add new header to response}
\DoxyCodeLine{  delete proxyRes.headers['x-\/removed'] // remove header from response}
\DoxyCodeLine{\}}

\end{DoxyCode}



\begin{DoxyItemize}
\item {\bfseries{option.\+on\+Proxy\+Req}}\+: function, subscribe to http-\/proxy\textquotesingle{}s {\ttfamily proxy\+Req} event.
\end{DoxyItemize}


\begin{DoxyCode}{0}
\DoxyCodeLine{function onProxyReq(proxyReq, req, res) \{}
\DoxyCodeLine{  // add custom header to request}
\DoxyCodeLine{  proxyReq.setHeader('x-\/added', 'foobar')}
\DoxyCodeLine{  // or log the req}
\DoxyCodeLine{\}}

\end{DoxyCode}



\begin{DoxyItemize}
\item {\bfseries{option.\+on\+Proxy\+Req\+Ws}}\+: function, subscribe to http-\/proxy\textquotesingle{}s {\ttfamily proxy\+Req\+Ws} event.
\end{DoxyItemize}


\begin{DoxyCode}{0}
\DoxyCodeLine{function onProxyReqWs(proxyReq, req, socket, options, head) \{}
\DoxyCodeLine{  // add custom header}
\DoxyCodeLine{  proxyReq.setHeader('X-\/Special-\/Proxy-\/Header', 'foobar')}
\DoxyCodeLine{\}}

\end{DoxyCode}



\begin{DoxyItemize}
\item {\bfseries{option.\+on\+Open}}\+: function, subscribe to http-\/proxy\textquotesingle{}s {\ttfamily open} event.
\end{DoxyItemize}


\begin{DoxyCode}{0}
\DoxyCodeLine{function onOpen(proxySocket) \{}
\DoxyCodeLine{  // listen for messages coming FROM the target here}
\DoxyCodeLine{  proxySocket.on('data', hybiParseAndLogMessage)}
\DoxyCodeLine{\}}

\end{DoxyCode}



\begin{DoxyItemize}
\item {\bfseries{option.\+on\+Close}}\+: function, subscribe to http-\/proxy\textquotesingle{}s {\ttfamily close} event. 
\begin{DoxyCode}{0}
\DoxyCodeLine{function onClose(res, socket, head) \{}
\DoxyCodeLine{  // view disconnected websocket connections}
\DoxyCodeLine{  console.log('Client disconnected')}
\DoxyCodeLine{\}}

\end{DoxyCode}

\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12364}{}\doxysubsection{http-\/proxy options}\label{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12364}
The following options are provided by the underlying \href{https://github.com/nodejitsu/node-http-proxy\#options}{\texttt{ http-\/proxy}} library.


\begin{DoxyItemize}
\item {\bfseries{option.\+target}}\+: url string to be parsed with the url module
\item {\bfseries{option.\+forward}}\+: url string to be parsed with the url module
\item {\bfseries{option.\+agent}}\+: object to be passed to http(s).request (see Node\textquotesingle{}s \href{http://nodejs.org/api/https.html\#https_class_https_agent}{\texttt{ https agent}} and \href{http://nodejs.org/api/http.html\#http_class_http_agent}{\texttt{ http agent}} objects)
\item {\bfseries{option.\+ssl}}\+: object to be passed to https.\+create\+Server()
\item {\bfseries{option.\+ws}}\+: true/false\+: if you want to proxy websockets
\item {\bfseries{option.\+xfwd}}\+: true/false, adds x-\/forward headers
\item {\bfseries{option.\+secure}}\+: true/false, if you want to verify the SSL Certs
\item {\bfseries{option.\+to\+Proxy}}\+: true/false, passes the absolute URL as the {\ttfamily path} (useful for proxying to proxies)
\item {\bfseries{option.\+prepend\+Path}}\+: true/false, Default\+: true -\/ specify whether you want to prepend the target\textquotesingle{}s path to the proxy path
\item {\bfseries{option.\+ignore\+Path}}\+: true/false, Default\+: false -\/ specify whether you want to ignore the proxy path of the incoming request (note\+: you will have to append / manually if required).
\item {\bfseries{option.\+local\+Address}} \+: Local interface string to bind for outgoing connections
\item {\bfseries{option.\+change\+Origin}}\+: true/false, Default\+: false -\/ changes the origin of the host header to the target URL
\item {\bfseries{option.\+preserve\+Header\+Key\+Case}}\+: true/false, Default\+: false -\/ specify whether you want to keep letter case of response header key
\item {\bfseries{option.\+auth}} \+: Basic authentication i.\+e. \textquotesingle{}user\+:password\textquotesingle{} to compute an Authorization header.
\item {\bfseries{option.\+host\+Rewrite}}\+: rewrites the location hostname on (301/302/307/308) redirects.
\item {\bfseries{option.\+auto\+Rewrite}}\+: rewrites the location host/port on (301/302/307/308) redirects based on requested host/port. Default\+: false.
\item {\bfseries{option.\+protocol\+Rewrite}}\+: rewrites the location protocol on (301/302/307/308) redirects to \textquotesingle{}http\textquotesingle{} or \textquotesingle{}https\textquotesingle{}. Default\+: null.
\item {\bfseries{option.\+cookie\+Domain\+Rewrite}}\+: rewrites domain of {\ttfamily set-\/cookie} headers. Possible values\+:
\begin{DoxyItemize}
\item {\ttfamily false} (default)\+: disable cookie rewriting
\item String\+: new domain, for example {\ttfamily cookie\+Domain\+Rewrite\+: \char`\"{}new.\+domain\char`\"{}}. To remove the domain, use {\ttfamily cookie\+Domain\+Rewrite\+: \char`\"{}\char`\"{}}.
\item Object\+: mapping of domains to new domains, use {\ttfamily \char`\"{}$\ast$\char`\"{}} to match all domains. ~\newline
 For example keep one domain unchanged, rewrite one domain and remove other domains\+: \`{}\`{}\`{} cookie\+Domain\+Rewrite\+: \{ \char`\"{}unchanged.\+domain\char`\"{}\+: \char`\"{}unchanged.\+domain\char`\"{}, \char`\"{}old.\+domain\char`\"{}\+: \char`\"{}new.\+domain\char`\"{}, \char`\"{}$\ast$\char`\"{}\+: \char`\"{}\char`\"{} \} \`{}\`{}{\ttfamily }
\end{DoxyItemize}
\item {\ttfamily $\ast$$\ast$option.cookie\+Path\+Rewrite$\ast$$\ast$\+: rewrites path of}set-\/cookie{\ttfamily headers. Possible values\+: -\/}false$<$tt$>$(default)\+: disable cookie rewriting
\begin{DoxyItemize}
\item String\+: new path, for examplecookie\+Path\+Rewrite\+: \char`\"{}/new\+Path/\char`\"{}{\ttfamily . To remove the path, use}cookie\+Path\+Rewrite\+: \char`\"{}\char`\"{}{\ttfamily . To set path to root use}cookie\+Path\+Rewrite\+: \char`\"{}/\char`\"{}{\ttfamily .}
\item {\ttfamily Object\+: mapping of paths to new paths, use}\char`\"{}$\ast$\char`\"{}\`{} to match all paths. For example, to keep one path unchanged, rewrite one path and remove other paths\+: \`{}\`{}\`{} cookie\+Path\+Rewrite\+: \{ \char`\"{}/unchanged.\+path/\char`\"{}\+: \char`\"{}/unchanged.\+path/\char`\"{}, \char`\"{}/old.\+path/\char`\"{}\+: \char`\"{}/new.\+path/\char`\"{}, \char`\"{}$\ast$\char`\"{}\+: \char`\"{}\char`\"{} \} \`{}\`{}{\ttfamily }
\end{DoxyItemize}
\item {\ttfamily $\ast$$\ast$option.headers$\ast$$\ast$\+: object, adds \mbox{[}request headers\mbox{]}(\href{https://en.wikipedia.org/wiki/List_of_HTTP_header_fields}{\texttt{ https\+://en.\+wikipedia.\+org/wiki/\+List\+\_\+of\+\_\+\+HTTP\+\_\+header\+\_\+fields}}\#Request\+\_\+fields). (Example\+:}\{host\+:\textquotesingle{}www.\+example.\+org\textquotesingle{}\}\`{})
\item {\bfseries{option.\+proxy\+Timeout}}\+: timeout (in millis) when proxy receives no response from target
\item {\bfseries{option.\+timeout}}\+: timeout (in millis) for incoming requests
\item {\bfseries{option.\+follow\+Redirects}}\+: true/false, Default\+: false -\/ specify whether you want to follow redirects
\item {\bfseries{option.\+self\+Handle\+Response}} true/false, if set to true, none of the web\+Outgoing passes are called and it\textquotesingle{}s your responsibility to appropriately return the response by listening and acting on the {\ttfamily proxy\+Res} event
\item {\bfseries{option.\+buffer}}\+: stream of data to send as the request body. Maybe you have some middleware that consumes the request stream before proxying it on e.\+g. If you read the body of a request into a field called \textquotesingle{}req.\+rawbody\textquotesingle{} you could restream this field in the buffer option\+:
\end{DoxyItemize}


\begin{DoxyCode}{0}
\DoxyCodeLine{'use strict';}
\DoxyCodeLine{}
\DoxyCodeLine{const streamify = require('stream-\/array');}
\DoxyCodeLine{const HttpProxy = require('http-\/proxy');}
\DoxyCodeLine{const proxy = new HttpProxy();}
\DoxyCodeLine{}
\DoxyCodeLine{module.exports = (req, res, next) => \{}
\DoxyCodeLine{}
\DoxyCodeLine{  proxy.web(req, res, \{}
\DoxyCodeLine{    target: 'http://localhost:4003/',}
\DoxyCodeLine{    buffer: streamify(req.rawBody)}
\DoxyCodeLine{  \}, next);}
\DoxyCodeLine{}
\DoxyCodeLine{\};}

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12365}{}\doxysection{Shorthand}\label{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12365}
Use the shorthand syntax when verbose configuration is not needed. The {\ttfamily context} and {\ttfamily option.\+target} will be automatically configured when shorthand is used. Options can still be used if needed.


\begin{DoxyCode}{0}
\DoxyCodeLine{proxy('http://www.example.org:8000/api')}
\DoxyCodeLine{// proxy('/api', \{target: 'http://www.example.org:8000'\});}
\DoxyCodeLine{}
\DoxyCodeLine{proxy('http://www.example.org:8000/api/books/*/**.json')}
\DoxyCodeLine{// proxy('/api/books/*/**.json', \{target: 'http://www.example.org:8000'\});}
\DoxyCodeLine{}
\DoxyCodeLine{proxy('http://www.example.org:8000/api', \{ changeOrigin: true \})}
\DoxyCodeLine{// proxy('/api', \{target: 'http://www.example.org:8000', changeOrigin: true\}); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12366}{}\doxysubsection{app.\+use(path, proxy)}\label{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12366}
If you want to use the server\textquotesingle{}s {\ttfamily app.\+use} {\ttfamily path} parameter to match requests; Create and mount the proxy without the http-\/proxy-\/middleware {\ttfamily context} parameter\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{app.use('/api', proxy(\{ target: 'http://www.example.org', changeOrigin: true \})) }

\end{DoxyCode}


{\ttfamily app.\+use} documentation\+:


\begin{DoxyItemize}
\item express\+: \href{http://expressjs.com/en/4x/api.html\#app.use}{\texttt{ http\+://expressjs.\+com/en/4x/api.\+html\#app.\+use}}
\item connect\+: \href{https://github.com/senchalabs/connect\#mount-middleware}{\texttt{ https\+://github.\+com/senchalabs/connect\#mount-\/middleware}}
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12367}{}\doxysection{Web\+Socket}\label{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12367}

\begin{DoxyCode}{0}
\DoxyCodeLine{// verbose api}
\DoxyCodeLine{proxy('/', \{ target: 'http://echo.websocket.org', ws: true \})}
\DoxyCodeLine{}
\DoxyCodeLine{// shorthand}
\DoxyCodeLine{proxy('http://echo.websocket.org', \{ ws: true \})}
\DoxyCodeLine{}
\DoxyCodeLine{// shorter shorthand}
\DoxyCodeLine{proxy('ws://echo.websocket.org') }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12368}{}\doxysubsection{External Web\+Socket upgrade}\label{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12368}
In the previous Web\+Socket examples, http-\/proxy-\/middleware relies on a initial http request in order to listen to the http {\ttfamily upgrade} event. If you need to proxy Web\+Sockets without the initial http request, you can subscribe to the server\textquotesingle{}s http {\ttfamily upgrade} event manually.


\begin{DoxyCode}{0}
\DoxyCodeLine{var wsProxy = proxy('ws://echo.websocket.org', \{ changeOrigin: true \})}
\DoxyCodeLine{}
\DoxyCodeLine{var app = express()}
\DoxyCodeLine{app.use(wsProxy)}
\DoxyCodeLine{}
\DoxyCodeLine{var server = app.listen(3000)}
\DoxyCodeLine{server.on('upgrade', wsProxy.upgrade) // <-\/-\/ subscribe to http 'upgrade' }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12369}{}\doxysection{Working examples}\label{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12369}
View and play around with \href{https://github.com/chimurai/http-proxy-middleware/tree/master/examples}{\texttt{ working examples}}.


\begin{DoxyItemize}
\item Browser-\/\+Sync (\href{https://github.com/chimurai/http-proxy-middleware/tree/master/examples/browser-sync/index.js}{\texttt{ example source}})
\item express (\href{https://github.com/chimurai/http-proxy-middleware/tree/master/examples/express/index.js}{\texttt{ example source}})
\item connect (\href{https://github.com/chimurai/http-proxy-middleware/tree/master/examples/connect/index.js}{\texttt{ example source}})
\item Web\+Socket (\href{https://github.com/chimurai/http-proxy-middleware/tree/master/examples/websocket/index.js}{\texttt{ example source}})
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12370}{}\doxysection{Recipes}\label{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12370}
View the \href{https://github.com/chimurai/http-proxy-middleware/tree/master/recipes}{\texttt{ recipes}} for common use cases.\hypertarget{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12371}{}\doxysection{Compatible servers}\label{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12371}
{\ttfamily http-\/proxy-\/middleware} is compatible with the following servers\+:


\begin{DoxyItemize}
\item \href{https://www.npmjs.com/package/connect}{\texttt{ connect}}
\item \href{https://www.npmjs.com/package/express}{\texttt{ express}}
\item \href{https://www.npmjs.com/package/browser-sync}{\texttt{ browser-\/sync}}
\item \href{https://www.npmjs.com/package/lite-server}{\texttt{ lite-\/server}}
\item \href{https://www.npmjs.com/package/grunt-contrib-connect}{\texttt{ grunt-\/contrib-\/connect}}
\item \href{https://www.npmjs.com/package/grunt-browser-sync}{\texttt{ grunt-\/browser-\/sync}}
\item \href{https://www.npmjs.com/package/gulp-connect}{\texttt{ gulp-\/connect}}
\item \href{https://www.npmjs.com/package/gulp-webserver}{\texttt{ gulp-\/webserver}}
\end{DoxyItemize}

Sample implementations can be found in the \href{https://github.com/chimurai/http-proxy-middleware/tree/master/recipes/servers.md}{\texttt{ server recipes}}.\hypertarget{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12372}{}\doxysection{Tests}\label{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12372}
Run the test suite\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{\# install dependencies}
\DoxyCodeLine{\$ npm install}
\DoxyCodeLine{}
\DoxyCodeLine{\# linting}
\DoxyCodeLine{\$ npm run lint}
\DoxyCodeLine{}
\DoxyCodeLine{\# unit tests}
\DoxyCodeLine{\$ npm test}
\DoxyCodeLine{}
\DoxyCodeLine{\# code coverage}
\DoxyCodeLine{\$ npm run cover }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12373}{}\doxysection{Changelog}\label{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12373}

\begin{DoxyItemize}
\item \href{https://github.com/chimurai/http-proxy-middleware/blob/master/CHANGELOG.md}{\texttt{ View changelog}}
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12374}{}\doxysection{License}\label{md_sfcode_node_modules_http_proxy_middleware_README_autotoc_md12374}
The MIT License (MIT)

Copyright (c) 2015-\/2018 Steven Chim 