8000 dev server: simple support for CORS requests by evanw · Pull Request #4171 · evanw/esbuild · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

dev server: simple support for CORS requests #4171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 6, 2025
Merged

dev server: simple support for CORS requests #4171

merged 1 commit into from
May 6, 2025

Conversation

evanw
Copy link
Owner
@evanw evanw commented May 5, 2025

Starting with version 0.25.0, esbuild's development server is no longer configured to serve cross-origin requests. This was a deliberate change to prevent any website you visit from accessing your running esbuild development server. However, this change prevented (by design) certain use cases such as "debugging in production" by having your production website load code from localhost where the esbuild development server is running.

To enable this use case, esbuild is adding a feature to allow Cross-Origin Resource Sharing (a.k.a. CORS) for simple requests. Specifically, passing your origin to the new cors option will now set the Access-Control-Allow-Origin response header when the request has a matching Origin header. Note that this currently only works for requests that don't send a preflight OPTIONS request, as esbuild's development server doesn't currently support OPTIONS requests.

Some examples:

  • CLI:

    esbuild --servedir=. --cors-origin=https://example.com
    
  • JS:

    const ctx = await esbuild.context({})
    await ctx.serve({
      servedir: '.',
      cors: {
        origin: 'https://example.com',
      },
    })
  • Go:

    ctx, _ := api.Context(api.BuildOptions{})
    ctx.Serve(api.ServeOptions{
      Servedir: ".",
      CORS: api.CORSOptions{
        Origin: []string{"https://example.com"},
      },
    })

The special origin * can be used to allow any origin to access esbuild's development server. Note that this means any website you visit will be able to read everything served by esbuild.

Fixes #4125

@evanw evanw merged commit e66cd0b into main May 6, 2025
16 checks passed
@evanw evanw deleted the cors branch May 6, 2025 00:18
@YangYongAn
Copy link

good job! there is no longer to need express to proxy !
image

@mbrevda
Copy link
mbrevda commented May 6, 2025

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make CORS configurable
3 participants
0