Задать вопрос
tony-stark77
@tony-stark77
Frontend developer (senior+)

Почему Axios делает POST запрос не на тот порт localhost?

Использую redux toolkit и createAsyncThunk

redux

import { createAsyncThunk, createSlice } from '@reduxjs/toolkit'
import axios from './../../utils/axios'

export const registerUser = createAsyncThunk(
	'auth/registerUser',
	async ({email, fullName, password, tel, garden}) => {
		try {
			console.log('sdsssssssssssss')
			const { data } = await axios.post('/auth/register', {
				fullName,
				password,
				email,
				tel,
				garden
			})
			console.log(data)
			if (data.token) {
				localStorage.setItem('token', data.token)
			}
			return data
		} catch (e) {
			console.log(e)
		}
	}
)

const initialState = {
	user: null,
	token: null,
	isLoading: null,
	status: null
}

export const authSlice = createSlice({
	name: 'auth',
	initialState,
	reducers: {},
	extraReducers: builder => {
		builder
			.addCase(registerUser.pending, (state, action) => {
				state.isLoading = true
				state.status = null
			})
			.addCase(registerUser.fulfilled, (state, action) => {
				console.log(action.payload)

				state.isLoading = false
				state.status = action.payload.message
				state.user = action.payload.user
				state.token = action.payload.token
			})
			.addCase(registerUser.rejected, (state, action) => {
				state.status = action.payload.message
				state.isLoading = false
			})
	}
})

export const {} = authSlice.actions

export default authSlice.reducer



axios

import axios from 'axios'

const instance = axios.create({
	baseUrl: 'http://localhost:5000/api'
})

instance.interceptors.request.use(config => {
	config.headers.Authorization = localStorage.getItem('token')
	return config
})

export default instance



react

const dispath = useDispatch()
	const [formValues, setFormValues] = useState({
		fullName: '',
		password: '',
		email: '',
		tel: '',
		garden: ''
	})

	const handleChange = event => {
		const { name, value } = event.target
		setFormValues({ ...formValues, [name]: value })
	}

	const handleSubmit = () => {
		try {
			dispath(registerUser(formValues))
			setFormValues({
				fullName: '',
				password: '',
				email: '',
				tel: '',
				garden: ''
			})
		} catch (err) {
			console.log(err)
		}
	}



Ошибка POST localhost:5173/auth/register 404 (Not Found)
сам бэк на 5000 порте
  • Вопрос задан
  • 371 просмотр
Подписаться 1 Простой Комментировать
Решения вопроса 1
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы