Skip to content
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

how to resolve CORS block in storybook ? #5

Open
shen-yu opened this issue Mar 11, 2020 · 3 comments
Open

how to resolve CORS block in storybook ? #5

shen-yu opened this issue Mar 11, 2020 · 3 comments
Labels
question Further information is requested

Comments

@shen-yu
Copy link

shen-yu commented Mar 11, 2020

hello, i'm configure this to aviod CORS block, but it's not work, how can i do?
image

image

@shen-yu
Copy link
Author

shen-yu commented Mar 11, 2020

I'm trying http-proxy-middleware package, but it's still not work.
Can you help me ? Thanks.
image

@aeksco
Copy link
Owner

aeksco commented Mar 12, 2020

@shen-yu Thanks for opening the ticket!

Looks like this can be solved by creating a custom Express.js application that serves Storybook as middleware - there's a helpful GitHub issue here documenting that process.

Looks like you can do something like this to proxy requests through your Express.js app:

var Express = require('express')
var proxy = require('http-proxy-middleware')
var storybook = require('@kadira/storybook/dist/server/middleware').default
var app = new Express()
var port = 4001
var storybookConfig = './.storybook'

app.use(storybook(storybookConfig))

app.use('/api', proxy({
  target: 'https://api-endpoint', // target host
  changeOrigin: true,
}))

app.listen(port, function (error) {
  if (error) {
    console.error(error)
  } else {
    console.info('==>  Listening on port %s. Open up http://localhost:%s/ in your browser.', port, port)
  }
})

I haven't tested this solution myself, but let's leave this ticket open - if anybody else would like to see this available as a core feature in the react-typescript-web-extension-starter please give this comment a 👍

@aeksco aeksco added the question Further information is requested label Mar 12, 2020
@aeksco
Copy link
Owner

aeksco commented Apr 15, 2021

I've found a good solution for this inside Storybook:

  • Add "http-proxy-middleware": "^1.0.4", dependency
  • Add .storybook/middleware.js:
const { createProxyMiddleware } = require("http-proxy-middleware");

// Adds Storybook server middleware to proxy requests to /api to http://localhost:3030
// The `http://localhost:3030` target is where local plugins are served when using @codotype/cli
module.exports = function expressMiddleware(router) {
    router.use(
        "/api",
        createProxyMiddleware({
            target: "http://localhost:3030",
            changeOrigin: true,
        }),
    );
};

If anyone wants to add this to the documentation in README.md it would be greatly appreciated!

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

No branches or pull requests

2 participants