Answer the question
In order to leave comments, you need to log in
What is an API in a react project?
For example, I have this code, let's say with action I have a request to add
axios.post(`/api/user/`, {
username: 'test',
email: '[email protected]',
password: 'faefe',
isEmailVerified: false,
verifyEmailToken: ''
});
router.route('/user')
.post((req, res) => {
const user = createUser(req.body);
user.save(err => {
if(err)
res.status(400).send(err);
res.json({ success: true });
});
});
Answer the question
In order to leave comments, you need to log in
You have 2 pieces of code from 2 different worlds:
1) this is the code from the frontend world where you make a request to /api/user/
2) this is the code from the backend world where you handle the ???/user route (why ???, because the main server file is not visible, but if you imagine that you are parsing some working example, most likely this is the /api/user route handler)
1. A server is made (in any of the languages), which, in principle, is a set of routes, for example:
GET на роут /products - отдает все продукты
POST на роут /products/add - создает новый продукт
GET на роут /proudcts/1231rh8fdf - отдает один продукт с id 1231rh8fdf
DELETE ...
PUT ...
export function signIn(email, password) {
return dispatch => {
dispatch({
type: USER_SIGN_IN_REQUEST,
})
const data = {
session: {
email,
password,
},
}
httpPost(`http://${API_ROOT_V1}/api/sessions`, data)
.then((data) => {
dispatch({
type: USER_SIGN_IN_SUCCESS,
data: data.account,
})
})
.catch((err) => {
console.warn(`Sign in error: ${err}`) //eslint-disable-line no-console
dispatch({
type: USER_SIGN_IN_FAILURE,
err,
})
})
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question