0

I managed to publish my app with nodejs and I am able to access it from devices in my network (mind you its going to be a local app only). It is an app that is used to control other hardware connected to my network.

The problem I am having right now is that, the app is working fine when I open it from 127.0.0.1:8081 from my local PC on which it is hosted, but if I attempt to open it from another device (or even from the hosting PC itself) by using the IP of the hosting PC 192.168.z.z:8081, the javascript files dont work and here is what I mean.

I have a config.js file which holds configuration data that my app needs to work, like IP of the device that I am controlling.

var config = {
    URL: '192.168.0.2',
}

Then I have a Model.js file that has SetData and GetData which are both dependent on config.URL to execute their AJAX requests, however config.URL stays null, its hardcoded and still a null!

When I run the app and do config.URL in the console, I get null, even if I manually set it in the console config.URL = '192.168.0.2', and then try to invoke Model.SetData..... or Model.GetData... they still dont see it, they still are attempting to send a request to http://null/......

Here is my node code:

const express = require("express");
const app = express();
const port = 8081

app.use(express.static('public'))
app.use('/css', express.static(__dirname + '/public/css'));
app.use('/js', express.static(__dirname + '/public/js'));

app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`));

I have tried with and without the app.use(/css and /js lines of code, no change

Here is how my JS is added to my html

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Robot Stuff</title>
  <script type="text/javascript" src="./js/jquery-3.5.0.js"></script>
  <script type="text/javascript" src="./assets/scripts/main.js"></script>
  <script type="text/javascript" src="./js/easy-numpad.js"></script>
  <script type="text/javascript" src="./js/jquery-modal.js"></script>
  <script type="text/javascript" src="./js/Model.js"></script>
  <script type="text/javascript" src="./js/UI.js"></script>
  <script type="text/javascript" src="./js/config.js"></script>
  <link href="./css/main.css" rel="stylesheet" type="text/css" >
  <link href="./css/easy-numpad.css" rel="stylesheet" type="text/css" >

Again, if I open that app from http://127.0.0.1:8081, or if I directly go and double-click on my index.html file, it all works just fine, but when I use the actual IP of the PC to access it remotely, then it breaks.

I dont want to overcomplicate it more than necessary, I am simply trying to use node explicitly to make the app available on my network, nothing more.

Darkbound
  • 3,026
  • 7
  • 34
  • 72
  • 1
    "then it breaks." What breaks? What error do you get when trying to access the app through a browser in another computer? – D. Pardal Apr 26 '20 at 13:43
  • It's not quite clear how and where you use your config.js and where you console.log(config.URL) – thammada.ts Apr 26 '20 at 13:48
  • @D.Pardal I have already explained what I mean by "it breaks", the javascript files cant get data from each other, see the explanation I gave about how my config and model js files work. – Darkbound Apr 26 '20 at 13:51
  • @TheeSritabtim I am not doing console.log(config.URL), I am doing that directly inside the Chrome browser Console after I open the app, config.URL to check its current value, which is null, but its actually hardcoded in the file, nevertheless it returns a null! So I manually set it (wiuthin the chrome console) config.URL = 'ipofdevice' and then I have another function startUpdating() which invokes the model.GetData, in model.GetData when I am executing my requests, the url is 'http://' + config.URL + '/therestoftheurl' – Darkbound Apr 26 '20 at 13:53
  • Just to confirm, the app and its JS files are being served from the same computer, right? – D. Pardal Apr 26 '20 at 13:55
  • @D.Pardal Yes, and actually I found the mistake, my config.js file was the last to be loaded, I moved it to the top and now it works, however I cant figure out why when it was the last file, it was working from 127.0.0.1 and not from 192.168.0.2 – Darkbound Apr 26 '20 at 13:56
  • 1
    For future reference: The Network tab of devtools is really helpful for troubleshooting this kind of stuff. – O. Jones Apr 26 '20 at 14:21

2 Answers2

1

You need to allow cross origin policy in your server script. That will enable you to accept request headers from your other devices also.

npm i cors --save

or alternatively you can follow this guide Enable CORS in nodejs

lousybear
  • 453
  • 3
  • 12
0

Check with netstat -nlp if your server is working on 0.0.0.0 or 127.0.0.1. This could possibly be one of the issue for non-accessibility outside localhost.