score:1
I find that with the "node express" and "http-proxy-middleware", it is feasible to use mock data and proxy api data at the same time. The details are as follows:
1) Create a mock.js file, which defines the request path and response body of mock datas:
const express = require('express');
const router = express.Router();
router.post('/mock/api/login', function (req, res, next) {
res.json({
"payload": {},
"errorMsg": {
"errorCode": null,
"errorMsg": ""
},
"status": 0
})
})
2) In main.js
const express = require('express');
const proxy = require('http-proxy-middleware');
const apiProxy = proxy({
target: 'http://192.168.1.40:8080',
changeOrigin: true
});
const app = express() ;
const mockDatas = require('./mock.js');
app.use('/', mockDatas);
app.use('/api', apiProxy);
score:2
I had a similar requirement where I needed to mock only a few calls and hit the original API for the others. For this, I hacked a tool Beeceptor for proxying and mocking at the same time. You can define mocking rule to decide when to mock, and the other calls are routed to original API.
This tool is language/platform agnostic as it is hosted and provides subdomain/endpoint.
(Disclaimer: I am the author of Beeceptor, and sharing here as it fulfills the precise need.)
Source: stackoverflow.com
Related Query
- Can I use mock data and proxy api data at the same time in my react project when using node express as a proxy server?
- I made an API call fetching data from it on a React component and passed it on to a state. I want to use the same data on another component too
- How can mock the value of a state and data in my react test
- How can I use react useContext , to show data from context, in the same component
- Can I use both ES6 and ES5 in the same React codebase?
- How can I use useEffect in React to run some code only when the component first mounts, and some other code whenever an event occurs (repeatedly)?
- Can you use React smooth scroll and React Router Links in the same website?
- How to refresh the table data when one of the item has its valid till date and time has expired in React Js
- React Router: props aren't passed down when directly accessing the URL of child component. Will I need to use the same state in both parent and child?
- How can I use the context provider api and typescript without correcting me all the time
- I have React application running in ASP.NET Core project and want to run both at the same time
- React - Make search and filter work on a given data at same time irrespective of the order of change
- How to combine the previous data with the new data in React and display it at the same time
- How can I turn the data that is a image came from API to url to use in img tag in react Js
- I want to use react hook 'useState' to save the information fetched from API and show on the scrren. But, I can't use in class so what can I do?
- Upload file (image) and data at the same time with react and node
- How do I use react context API to pass data from parent component to child component when using react-router in the parent component in React.js?
- how can i send an api call from the supreme function to another component called product to change a mock data called cart: true and display in cart
- my api needs time to process a request, how can I use React + SWR to continue checking on the status?
- Pass the fetched data from an API and use it to style buttons using React and Sass
- Can conditional rendering in React use logical operator || when rendering the same content?
- React functional stateless component, PureComponent, Component; what are the differences and when should we use what?
- useState hook can only set one object at the time and return the other object of the same array to initial state
- Can I use the React Context API inside a Context API or do I have to merge them?
- How to use the same port for React Js and Node Js?
- How to use React.FC<props> type when the children can either be a React node or a function
- How can I start my node server and react app at the same time?
- Can we cancel out api response when the react router is changed?
- In React componentDidUpdate, can props and state change at the same time?
- How can I cache data that I already requested and access it from the store using React and Redux Toolkit
More Query from same tag
- Server Side Render re-render at client
- Unable to change the legend symbol to rounded square in chartjs in React
- Looking for a way to display different text in a Modal based off of which button is pressed
- JavaScript On-click Function to start and stop Intervals
- React global variable not being updated inside function that passing to children component
- Disable next button while API data is fetching- DataGrid Material UI
- How to check if url has certain part in react router dom?
- React – can't filter array properly
- Calculated row: How to calculate cell value of the particular column based on the values from other rows in the same column using AG Grid?
- Sorted items of array not rendering properly with react
- How do I hide an element if flash has not been detected in React JSX
- useState Hook trigger selected rows to disappear (using react-bootstrap-table next)
- Console Log from Props
- React state property undefined in function component using Context API
- Passing in key through `map` function call
- React functional component doesnt rerender when changing props passed as an array
- React UseState()
- Auto update the user interface in ReactJS
- React: how to update an array in a usestate
- GraphQL / Relay - do all tables need to be queried in main QueryRenderer?
- Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment -ReactJS
- How do I get collectstatic to work when running a Django/React App on Heroku?
- react useState enters infinite loop even thou variable is the same
- Why do these uncontrolled radio inputs require two clicks to be selected?
- React functional component setState not updating the state
- Required babel-jest dependency
- Flex-end and start not aligning vertically
- Access the latest state from external function React JS
- Getting 'React' must be in scope when using JSX
- Why does Edge not accept `undefined` in class instantiation but Chrome does? How do I fix?