Storybook: How to make a REST API call from a story.

Created on 7 May 2018  路  5Comments  路  Source: storybookjs/storybook

Hi Team,

I wanted to make a REST call from the story in order to fetch the data from server but the port of storybook gets in the REST call.

How I can configure storybook to access the server?

Thanks in Advance.

Amitt

question / support

Most helpful comment

Thanks for the pointers guys.

I used middleware extension storybook. Created a file middleware.js in .storybook folder with the following code:

const proxy = require('http-proxy-middleware')
module.exports = function expressMiddleware (router) {
router.use('/api', proxy({
target: 'http://localhost:8090',
changeOrigin: true
}))
}
using the http-proxy-middleware I was able to make all the /api to the server (where target is the server from where data is expected)

Thanks
Amit

All 5 comments

I believe in development you can make use of webpack devServer.proxy option to proxy all API calls to your back-end server.

You can do the same in production but not through webpack, external proxy will do the job

What I do in my project is use absolute URLs in requests from stories

Thanks, Can you please provide the example or some pointers how to set it up? In Webpack.config.js and packages.json? Do I need to install some special package for the same?

I mean, I pass the full URL 'http://my-web-app.address/rest/endpoint'

Thanks for the pointers guys.

I used middleware extension storybook. Created a file middleware.js in .storybook folder with the following code:

const proxy = require('http-proxy-middleware')
module.exports = function expressMiddleware (router) {
router.use('/api', proxy({
target: 'http://localhost:8090',
changeOrigin: true
}))
}
using the http-proxy-middleware I was able to make all the /api to the server (where target is the server from where data is expected)

Thanks
Amit

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tomitrescak picture tomitrescak  路  3Comments

xogeny picture xogeny  路  3Comments

ZigGreen picture ZigGreen  路  3Comments

tirli picture tirli  路  3Comments

dnlsandiego picture dnlsandiego  路  3Comments