My server is running on localhost:5000 and my iPhone is connected to metro server with tunnel method using Expo. I tried to change localhost with the ip address of my Pc but the log is the following:
Network request failed
at node_modules\whatwg-fetch\dist\fetch.umd.js:535:17 in setTimeout$argument_0
at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:130:14 in _callTimer
at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:383:16 in callTimers
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:416:4 in __callFunction
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:109:6 in __guard$argument_0
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:364:10 in __guard
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:108:4 in callFunctionReturnFlushedQueue
at [native code]:null in callFunctionReturnFlushedQueue
the code of my fetch function is:
const onSubmitHandler = () => {
const payload = {
phone,
name,
surname,
password,
city,
dateOfBirth,
};
fetch("http://192.168.1.40:5000/signup", {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
})
[....]
}
} catch (err) {
console.log(err);
};
})
.catch(err => {
console.log(err);
});
};
and my index.js of server is:
import express from 'express';
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
import sequelize from './utils/database.js';
import router from './routes/routes.js';
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use((_, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
next();
});
app.use(router);
sequelize.sync();
app.listen(5000);
How I can fix this problem?