简体   繁体   中英

Moved mongoDB externally from it being local, now app showing ERR CONNECTION

I recently had an app where I had MongoDB saved locally to my PC and it worked like a dream, I have now altered this to enable to DB to be accessed externally but now I'm having connection issues, please could someone point out where I have gone wrong.

within my server.js file I have the below:

var uri = "mongodb+srv://kayleigh:notMyRealPassword@cluster0-ikkkw.mongodb.net/Hangman?retryWrites=true&w=majority";

The error in Chrome's console is: GET http://localhost:9000/random net::ERR_CONNECTION_REFUSED

Within my file called script.js I have some GET requests where I think may be the cause of the issue, do I need to replace the section where it states "http://localhost:9000/getPlayers" ??

// search database for the players name that's just been entered
$.get("http://localhost:9000/getPlayers", function(data) {
    console.log(JSON.stringify(data));

    var counter = 0;

    // loop through each player (value) within list of players (data)
    $.each(data, function(key, value){
        // if the name is equal to person name, increment counter.
        if (value.name == personName)
        {
            counter++;

            // updating player score into DB by 5 each win.
            let playerData = {"name": personName, "score": value.score + personScore};

            updatePlayerScore(playerData);
            $("#wonOrLost").text("Your score has been updated on the leadership board!");
            return false;
        }
    });

    if (counter == 0){
        // Not in database, so adding new player.
        let playerData = {"name": personName, "score": personScore};

        addPlayer(playerData);
        $("#wonOrLost").text("Your score has been added to the leadership board!");
    }
});

server.js file also contains this /random code (which gets a random word from mongoDB):

app.get("/random", async function(request, response) {
    var randomWord = await random();
    response.send(randomWord.text);
});

// This function goes to my db and counts all of the words in there to randomly choose the ID 
// that's associated with a word. It will return the String.
async function random () {
    var words = await db.getWords();
    var count = words.length;
    var randomId = Math.floor(Math.random() * count);
    console.log(randomId);
    var randomWord = await db.getWordById(randomId);
    console.log(randomWord);
    return randomWord;
}

So the connection error you are currently getting has nothing to do with mongodb. Your server.js looks to be part of a nodejs express server that's what will be being called by the http://localhost:9000/getPlayers . (your express server is hosting this webpage)

If you have moved both your mongo and your express web server to the new server you would need to change localhost with the IP of your new external server so it would be http://:9000/getPlayers. (im assuming your calling it using jquery from a html page from your code)

Other things to watch out for here also include your new servers firewall (9000 is not a standard web server port) and if your setting the bind address in your express server (app.listen(9000, '127.0.0.1') the 127.0.0.1 would be the bind address) then set that to the servers public IP also.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM