vite icon indicating copy to clipboard operation
vite copied to clipboard

feat(proxy): add router to support dynamically set the target

Open Fatpandac opened this issue 1 year ago • 6 comments

Description

add router option to support dynamically set the target

Additional context

URL example: /dynamicProxy/localhost:9607/anotherApp

Configuration

server: {
	proxy: {
		'/dynamicProxy': {
			target: 'http://localhost:9600',
			router: (req) => {
				const target = req.url.match(/\/dynamicProxy\/(.*?)\//)[1]
				
				return `http://${target}`
			},
			rewrite: (path) => path.replace(/^\/dynamicProxy\/.*?\//, ''),
			changeOrigin: true,
		}
	}
}

What is the purpose of this pull request?

  • [ ] Bug fix
  • [X] New Feature
  • [ ] Documentation update
  • [ ] Other

Before submitting the PR, please make sure you do the following

  • [X] Read the Contributing Guidelines.
  • [X] Read the Pull Request Guidelines and follow the PR Title Convention.
  • [X] Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • [X] Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • [X] Ideally, include relevant tests that fail without this PR but pass with it.

Fatpandac avatar Jul 27 '23 16:07 Fatpandac