'Send a CORS request to Localhost from an electron application

I'm working on a project that involves an application built with Electron that interfaces with an express server, running on Localhost or the home network. Problem right now is, I'm having trouble getting the server to acknowledge any requests from the application.

Here is my front end logic in the electron application:

    let ipAddress;
let port;
let requestAddress;

function connect(){
    const ipField = document.getElementById("nu-ip").value;
    const portField = document.getElementById("nu-port").value;
    port = portField;
    if (ipField === "") {
        ipAddress = 'localhost';
    } else {
        ipAddress = ipField;
    }
    port = portField;
    if(port === ""){

        requestAddress = `http://$(ipAddress)`;
    } else {
        requestAddress = `http://${ipAddress}:${port}`;
    };
    alert(requestAddress);
    const request = newXMLHttpRequest();
    alert(requestAddress);
    request.open("GET",`${requestAddress}/connect`).send();
    request.onReadyStateChange = (res) => {
        alert(res);
    }
}
function startup() {
    console.log('Hey where does this show up?')
    const NuToggle = document.getElementById("NuHelper-enable");
    const NuTools = document.getElementById("Nu-tools");
    const connectButton = document.getElementById("connect-button");
    NuToggle.addEventListener("change", (event) => {
        if(event.target.value === 'enable'){
            //alert("NuHelper has been enabled");
            NuTools.style.display='block';
            connectButton.addEventListener('click', connect);
        }
    })
    
}

window.onload = startup;

And here is my server:

//require in our basic dependencies
const express = require('express');
const morgan = require('morgan');
const bodyParser = require('body-parser');
const errorHandler = require('errorhandler');
const cors = require('cors');

const PORT = 80;
const app = express();
app.use(morgan('dev'));
app.use(bodyParser);
app.use(errorHandler);
app.use(cors());

app.get('/connect',(req, res, next) => {
    res.sendStatus(200);
})
app.listen(PORT, () => {
    console.log(`Nu is listening on PORT ${PORT}`);
})

I put 80 into the PORT input and it'll alert "http://localhost:80", but it'll get no response at all from the server, and my logging middleware won't acknowledge that it received any request at all, which makes me think that I'm sending the request to the wrong address. Thanks in advance to anyone who understands how to solve this!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source