Home / Function/ proxyMiddleware() — vite Function Reference

proxyMiddleware() — vite Function Reference

Architecture documentation for the proxyMiddleware() function in proxy.ts from the vite codebase.

Entity Profile

Dependency Diagram

graph TD
  65695310_3d20_5d70_c2ad_710df99d8481["proxyMiddleware()"]
  d873c697_620e_ffca_0134_e9fecd784782["proxy.ts"]
  65695310_3d20_5d70_c2ad_710df99d8481 -->|defined in| d873c697_620e_ffca_0134_e9fecd784782
  5c50110b_5c76_c14f_b1dd_3efd3df7f375["preview()"]
  5c50110b_5c76_c14f_b1dd_3efd3df7f375 -->|calls| 65695310_3d20_5d70_c2ad_710df99d8481
  24ecf2a1_3c09_d451_76f3_9485b4e993f8["_createServer()"]
  24ecf2a1_3c09_d451_76f3_9485b4e993f8 -->|calls| 65695310_3d20_5d70_c2ad_710df99d8481
  71bee4b5_1093_f98d_06c8_78977e4eb508["rewriteOriginHeader()"]
  65695310_3d20_5d70_c2ad_710df99d8481 -->|calls| 71bee4b5_1093_f98d_06c8_78977e4eb508
  96889b1a_4e14_1c3f_dbfe_aa244646006f["doesProxyContextMatchUrl()"]
  65695310_3d20_5d70_c2ad_710df99d8481 -->|calls| 96889b1a_4e14_1c3f_dbfe_aa244646006f
  style 65695310_3d20_5d70_c2ad_710df99d8481 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/vite/src/node/server/middlewares/proxy.ts lines 75–229

export function proxyMiddleware(
  httpServer: HttpServer | null,
  options: NonNullable<CommonServerOptions['proxy']>,
  config: ResolvedConfig,
): Connect.NextHandleFunction {
  // lazy require only when proxy is used
  const proxies: Record<string, [httpProxy.ProxyServer, ProxyOptions]> = {}

  Object.keys(options).forEach((context) => {
    let opts = options[context]
    if (!opts) {
      return
    }
    if (typeof opts === 'string') {
      opts = { target: opts, changeOrigin: true }
    }
    const proxy = httpProxy.createProxyServer(opts)

    if (opts.configure) {
      opts.configure(proxy, opts)
    }

    proxy.on('error', (err, _req, res) => {
      // When it is ws proxy, res is net.Socket
      if ('req' in res) {
        config.logger.error(
          `${colors.red(`http proxy error: ${res.req.url}`)}\n${err.stack}`,
          {
            timestamp: true,
            error: err,
          },
        )
        if (!res.headersSent && !res.writableEnded) {
          res
            .writeHead(500, {
              'Content-Type': 'text/plain',
            })
            .end()
        }
      } else {
        config.logger.error(`${colors.red(`ws proxy error:`)}\n${err.stack}`, {
          timestamp: true,
          error: err,
        })
        res.end()
      }
    })

    proxy.on('proxyReqWs', (proxyReq, _req, socket, options) => {
      rewriteOriginHeader(proxyReq, options, config)

      socket.on('error', (err) => {
        config.logger.error(
          `${colors.red(`ws proxy socket error:`)}\n${err.stack}`,
          {
            timestamp: true,
            error: err,
          },
        )
      })
    })

    // clone before saving because http-proxy mutates the options
    proxies[context] = [proxy, { ...opts }]
  })

  if (httpServer) {
    httpServer.on('upgrade', async (req, socket, head) => {
      const url = req.url!
      for (const context in proxies) {
        if (doesProxyContextMatchUrl(context, url)) {
          const [proxy, opts] = proxies[context]
          if (
            opts.ws ||
            opts.target?.toString().startsWith('ws:') ||
            opts.target?.toString().startsWith('wss:')
          ) {
            if (opts.bypass) {
              try {
                const bypassResult = await opts.bypass(req, undefined, opts)
                if (typeof bypassResult === 'string') {

Domain

Subdomains

Frequently Asked Questions

What does proxyMiddleware() do?
proxyMiddleware() is a function in the vite codebase, defined in packages/vite/src/node/server/middlewares/proxy.ts.
Where is proxyMiddleware() defined?
proxyMiddleware() is defined in packages/vite/src/node/server/middlewares/proxy.ts at line 75.
What does proxyMiddleware() call?
proxyMiddleware() calls 2 function(s): doesProxyContextMatchUrl, rewriteOriginHeader.
What calls proxyMiddleware()?
proxyMiddleware() is called by 2 function(s): _createServer, preview.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free