Что я пробовал
- Изменен URL-адрес API в .env.development:
- (для эмулятора Android)
Код: Выделить всё
API_URL=http://10.0.2.2:3000 - (мой локальный IP-адрес)
Код: Выделить всё
API_URL=http://192.xxx.x.x:3000 Код: Выделить всё
API_URL=http://localhost:3000
- Обновлен файл app.json, чтобы разрешить трафик в виде открытого текста:
Код: Выделить всё
"expo": { "android": { "usesCleartextTraffic": true } } - Подтверждено, что серверная часть работает:
- < li>Доступ к http://localhost:3000 в моем браузере работает нормально.
Отключил брандмауэр/антивирус, чтобы они не блокировали запросы.
- < li>Доступ к http://localhost:3000 в моем браузере работает нормально.
- Проверил URL-адрес API в Postman с использованием моего локального IP-адреса, и он работает.
.env.development:
Код: Выделить всё
#API_URL for web
#API_URL=http://localhost:3000
#API_URL for android
API_URL=http://10.0.2.2:3000
auth.ts:
Код: Выделить всё
export const login = async (email: string, password: string) => {
console.log("API_URL", API_URL);
const response = await fetch(`${API_URL}/auth/login`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ email, password }),
});
const data = await response.json();
if (!response.ok) {
throw new Error(data.error);
}
return data;
};
Код: Выделить всё
// CORS configuration to allow requests from the frontend
const corsOptions = {
origin: true,
credentials: true, // Allow credentials (cookies, headers)
};
app.use(cors(corsOptions));
// Handle preflight requests for all routes
app.options('*', cors(corsOptions));
Подробнее здесь: https://stackoverflow.com/questions/790 ... ss-backend
Мобильная версия