React+Node.js注册/登录遇Network request failed及404错误求助
React前端+Node.js后端注册/登录网络错误问题
用React开发前端应用、Node.js搭建后端,注册或登录时持续遭遇相同的网络错误TypeError: Network request failed。Postman测试完全正常,说明后端、数据库及前端传值逻辑无问题,但浏览器提示:
"register:1 Failed to load resource: the server responded with a status of 404 (Not Found)"
SignUpScreen.js
function SignUpScreen(props) { const navigation = useNavigation(); const navigateToSignIn = () => { navigation.navigate('Sign In'); }; const handleRegister = async () => { console.log('Start handleRegister'); try { const response = await fetch('http://localhost/register', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ Email, password }), }); console.log('After fetch'); const data = await response.json(); };
Backend code
app.post('/register', async (req, res) => { try { const { email, password } = req.body; const saltRounds = 10; const hashedPassword = await bcrypt.hash(password, saltRounds); const user = { email, password: hashedPassword }; const insertUserQuery = 'INSERT INTO users (email, password) VALUES (?, ?)'; });
已尝试以下方案但未解决:
- 将“localhost”替换为IP地址
- 使用CORS
- 在Android清单文件
android/app/src/main/AndroidManifest.xml中添加android:usesCleartextTraffic="true" - 更换不同物理设备
内容的提问来源于stack exchange,提问作者steez
相关产品推荐
相关产品推荐

