Set default axios baseURL for call API
Open .env file and edit VITE_APP_API_URL.
## Backend API URL
VITE_APP_API_URL=
// axios
import axios from 'axios';
const axiosServices = axios.create({ baseURL: import.meta.env.VITE_APP_API_URL || 'http://localhost:3010/' });
// ==============================|| AXIOS SERVICES ||============================== //
axiosServices.interceptors.response.use(
(response) => response,
(error) => {
if (error.response.status === 401 && !window.location.href.includes('/login')) {
window.location.pathname = '/login';
}
return Promise.reject((error.response && error.response.data) || 'Wrong Services');
}
);
export default axiosServices;
Example 2: Without baseUrl
You can set the entire URL in Axios request. Do not use common Axios instances src\utils\axios.js
instead use directly Axios library.
// axios
import axios from 'axios';
const axiosServices = axios.create();
// ==============================|| AXIOS SERVICES ||============================== //
axiosServices.interceptors.response.use(
(response) => response,
(error) => Promise.reject((error.response && error.response.data) || 'Wrong Services')
);
export default axiosServices;