Answer the question
In order to leave comments, you need to log in
What is React Mock API? And how to use it?
Good day!
There is a react application with axios requests to the server. Sometimes it happens that the server stops working. I was told to 'mock' api methods on the client in order to be able to work without a server.
What does it mean? Do I understand correctly that you can add code that will intercept api requests and return the data that I specify? As I understand it, this is used for testing (testing articles are issued on the Internet for the request "react mock api"). Is it possible to apply this not for testing, but for working without an active server?
I would be grateful for explanations and links to resources where you can read about all this.
Answer the question
In order to leave comments, you need to log in
What is React Mock API?
A request to the server api is asynchronous functions that send a request and wait for a response, respectively, you can replace them with your own, for example, you have an object with server request functions
const serverAPI = {
getCategories: () => fetch('/categories'),
getCategory: (id) => fetch('/categories/' + id)
}
// Создадим базу данных
const mockDB = {
categories: [
{ id: 1, name: 'products'},
{ id: 2, name: 'services'}
]
}
const mockAPI = {
getCategories: () => Promise.resolve(mockDB.categories),
getCategory: (id) => Promise.resolve(mockDB.categories.find(v => v.id === id)),
}
Object.assign(currentAPI, mockAPI)
Object.assign(currentAPI, serverAPI)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question