0

I'm just starting out experimenting with NodeJS 10.1.0 and MongoDB 3.6.5 together. My first goal is to have my NodeJS app create a database. Very straightforward:

main.js:

var http = require("http");

var databaseCreator = require("./databaseCreator");

http.createServer(databaseCreator.createDatabase).listen(3000);

console.log('Server running at http://127.0.0.1:3000/')

databaseCreator.js

var mongodb = require("mongodb");

exports.createDatabase = function (request, response) {
    var client = mongodb.MongoClient;
    var connectionURL = "mongodb://localhost:3000/test";

    client.connect(connectionURL, function(err, db) {
        if (err) throw err;
        db.close();

        console.log("Database created!");

    });

    response.writeHead(200, {'Content-Type': 'text/plain'});
    response.write('Database created!');
    response.end();

}

When I run the app and point my browser at the server, I get this on the terminal:

Server running at http://127.0.0.1:3000/
(node:3489) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.

/home/linksys/linksys/node_modules/mongodb/lib/mongo_client.js:891
          throw err;
          ^
[object Object]

Here is my package-lock.json:

{
  "requires": true,
  "lockfileVersion": 1,
  "dependencies": {
    "bson": {
      "version": "1.0.6",
      "resolved": "https://registry.npmjs.org/bson/-/bson-1.0.6.tgz",
      "integrity": "sha512-D8zmlb46xfuK2gGvKmUjIklQEouN2nQ0LEHHeZ/NoHM2LDiMk2EYzZ5Ntw/Urk+bgMDosOZxaRzXxvhI5TcAVQ=="
    },
    "mongodb": {
      "version": "3.1.0-beta4",
      "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.1.0-beta4.tgz",
      "integrity": "sha512-nJAK59fFlMWTdEJaTyGp3HeerkIahnupoMNjbszPJVnO63/36aFnlWHqpYrrJwj9GKlmb47YWBIvNo6fdrUL4Q==",
      "requires": {
        "mongodb-core": "3.1.0-beta4"
      }
    },
    "mongodb-core": {
      "version": "3.1.0-beta4",
      "resolved": "https://registry.npmjs.org/mongodb-core/-/mongodb-core-3.1.0-beta4.tgz",
      "integrity": "sha512-aiwUKBGmFZwBx4CdC1iK5kjZyk0zhDxQ0edRtpTZUkk1jyWkdzYd6GEA6wMl7O6ZX/dOiFM2ujkrUR7+vkqJPw==",
      "requires": {
        "bson": "~1.0.4",
        "require_optional": "^1.0.1",
        "saslprep": "^1.0.0"
      }
    },
    "require_optional": {
      "version": "1.0.1",
      "resolved": "https://registry.npmjs.org/require_optional/-/require_optional-1.0.1.tgz",
      "integrity": "sha512-qhM/y57enGWHAe3v/NcwML6a3/vfESLe/sGM2dII+gEO0BpKRUkWZow/tyloNqJyN6kXSl3RyyM8Ll5D/sJP8g==",
      "requires": {
        "resolve-from": "^2.0.0",
        "semver": "^5.1.0"
      }
    },
    "resolve-from": {
      "version": "2.0.0",
      "resolved": "https://registry.npmjs.org/resolve-from/-/resolve-from-2.0.0.tgz",
      "integrity": "sha1-lICrIOlP+h2egKgEx+oUdhGWa1c="
    },
    "saslprep": {
      "version": "1.0.0",
      "resolved": "https://registry.npmjs.org/saslprep/-/saslprep-1.0.0.tgz",
      "integrity": "sha512-5lvKUEQ7lAN5/vPl5d3k8FQeDbEamu9kizfATfLLWV5h6Mkh1xcieR1FSsJkcSRUk49lF2tAW8gzXWVwtwZVhw==",
      "optional": true
    },
    "semver": {
      "version": "5.5.0",
      "resolved": "https://registry.npmjs.org/semver/-/semver-5.5.0.tgz",
      "integrity": "sha512-4SJ3dm0WAwWy/NVeioZh5AntkdJoWKxHxcmyP622fOkgHa4z3R0TdBJICINyaSDE6uNwVc8gZr+ZinwZAH4xIA=="
    }
  }
}

Does anyone know what is happening here? Visiting the file and line number listed, I find I'm in the middle of some kind of handler, but I don't have enough experience to figure things out more than that, sorry.

MadEmperorYuri
  • 298
  • 3
  • 11
  • `mongodb://localhost:3000/` That won't be the port MongoDB is running on, and it looks like where you are attempting to run your program on. Did you actually install the mongodb "server"? Or did you just install the driver with `npm` and thought that would work? – Neil Lunn May 26 '18 at 01:04
  • This looks like the part where you get directed to read the manual that goes with the package [Quick Start - Start a MongoDB server](http://mongodb.github.io/node-mongodb-native/3.0/quick-start/quick-start/#start-a-mongodb-server) – Neil Lunn May 26 '18 at 01:06

1 Answers1

0

The port number was incorrect. I had Mongo configured to pay attention to port 3001, not 3000. Thanks to @neil-lunn for spotlighting it.

MadEmperorYuri
  • 298
  • 3
  • 11