0
votes

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

1
What is the network error? And what makes you think that this is an issue on the server side? - goto1
It just says Network error, but like I said, I tried a fake api website (reqres.in) instead of localhost and it worked - Mrkinix
Is there a more detailed description of what the error says? Perhaps look in the network tab - I can't replicate it on my end with the same server setup you have above. - goto1
Paste your logs, that should have a better description of your error - Ramaraja Ramanujan
I updated my post, you can see the error in the end. - Mrkinix

1 Answers

0
votes

I fixed it by changing node port from localhost to my primary ip adress

app.listen(process.env.PORT || 8081, 192.168.100.3);