Skip to content

Instantly share code, notes, and snippets.

@nwellis
Created February 25, 2023 15:04
Show Gist options
  • Select an option

  • Save nwellis/1ec03199e587bc73dea979cc118d137e to your computer and use it in GitHub Desktop.

Select an option

Save nwellis/1ec03199e587bc73dea979cc118d137e to your computer and use it in GitHub Desktop.
Proxy server for Astro dev server
import { createProxyMiddleware } from "http-proxy-middleware"
/**
* Usage:
* ```js
* proxyMiddleware("proxy/path", {
* target: "https://target.proxy.io",
* changeOrigin: true,
* }),
* ```
*
* @param {Parameters<createProxyMiddleware>[0]} context
* @param {Parameters<createProxyMiddleware>[1]} options
*
* @returns @type {import('astro').AstroIntegration}
* @see https://www.npmjs.com/package/http-proxy-middleware
*/
export default (context, options) => {
const apiProxy = createProxyMiddleware(context, options)
/** @type {import('astro').AstroIntegration} */
const integration = {
name: 'proxy-middleware',
hooks: {
'astro:server:setup': ({ server }) => {
server.middlewares.use(apiProxy)
}
}
}
return integration
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment