I always get a Network error when I try to get data from app.js using axios, here is my code:
//app.js
var express = require('express');
var app = express();
const cors = require('cors');
var morgan = require('morgan')
const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(morgan('combined'));
app.use(cors());
app.get('/lol', (req, res) =>{
res.send({
message: 'lol'
});
});
app.listen('8800');
console.log('started on port 8800');
// axios - Api.js
import axios from 'axios';
export default() => {
return axios.create({
baseURL: `http://localhost:8800`
});
};
- PostService.js
import Api from './Api.js';
export default {
async test () {
const result = await Api().get('/lol')
return (result.data.message);
},
};
// HomeScreen.js (react native)
export default function HomeScreen() {
....
async function wiw() {
try{
const responce = await Api.test()
console.log(`responce= ${responce}`)
alert(`responce= ${responce}`)
} catch(e) {
console.log(e)
}
}
I get network error, however if I this (https://reqres.in/api/users?page=2) as my API it works fine, but the problem is with my node app ( when I test it with PostMan, it works as expected)
Edit: axios doesn't seem to reach node, I added console.log to '/lol' whenever I call it it doesn't log anything, here is the error I get:
Network Error - node_modules/axios/lib/core/createError.js:16:24 in createError - node_modules/axios/lib/adapters/xhr.js:81:25 in handleError - node_modules/event-target-shim/dist/event-target-shim.js:818:39 in dispatchEvent - node_modules/react-native/Libraries/Network/XMLHttpRequest.js:574:29 in setReadyState - node_modules/react-native/Libraries/Network/XMLHttpRequest.js:388:25 in __didCompleteResponse - node_modules/react-native/Libraries/vendor/emitter/EventEmitter.js:190:12 in emit - node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:395:47 in __callFunction - node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:106:26 in - node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:343:10 in __guard - node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:105:17 in callFunctionReturnFlushedQueue * [native code]:null in callFunctionReturnFlushedQueue