Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MongoDB connection error: Error: slash in host identifier #2

Open
amoungui opened this issue Apr 18, 2020 · 6 comments
Open

MongoDB connection error: Error: slash in host identifier #2

amoungui opened this issue Apr 18, 2020 · 6 comments

Comments

@amoungui
Copy link

amoungui commented Apr 18, 2020

Hello, I'm running your node.js project with mongodb, express, but I get this error when I start the server:

MongoDB connection error: Error: slash in host identifier
[nodemon] app crashed - waiting for file changes before starting ...

this is my files:
mongoose.js file:

const { mongo, env } = require('./vars');

// set mongoose Promise to Bluebird
mongoose.Promise = Promise;

// Exit application on error
mongoose.connection.on('error', (err) => {
  console.error(`MongoDB connection error: ${err}`);
  process.exit(-1);
});

// print mongoose logs in dev env
if (env === 'development') {
  mongoose.set('debug', true);
}

/**
* Connect to mongo db
*
* @returns {object} Mongoose connection
* @public
*/
exports.connect = () => {
  mongoose.connect(mongo.uri, {
    keepAlive: 1,
    useMongoClient: true,
  });
  return mongoose.connection;
};

my var.js file:

const path = require('path');

// import .env variables
require('dotenv-safe').load({
  path: path.join(__dirname, '../../.env'),
  sample: path.join(__dirname, '../../.env.example'),
});

module.exports = {
  env: process.env.NODE_ENV,
  port: process.env.PORT,
  jwtSecret: process.env.JWT_SECRET,
  jwtExpirationInterval: process.env.JWT_EXPIRATION_MINUTES,
  masterAccount: process.env.MASTER_ACCOUNT_NUMBER,
  masterAccountPassword: process.env.MASTER_ACCOUNT_PASSWORD,
  mongo: {
    uri: process.env.NODE_ENV === 'test'
      ? process.env.MONGO_URI_TESTS
      : process.env.MONGO_URI,
  },
  logs: process.env.NODE_ENV === 'production' ? 'combined' : 'dev',
};

the index.js file:

Promise = require('bluebird'); // eslint-disable-line no-global-assign
const { port, env } = require('./config/vars');
const app = require('./config/express');
const mongoose = require('./config/mongoose');

// open mongoose connection
mongoose.connect();

// listen to requests
app.listen(port, () => console.info(`server started on port ${port} (${env})`));

/**
* Exports express
* @public
*/
module.exports = app;

thanks for you help

@Ethical-Ralph
Copy link

Ethical-Ralph commented Apr 20, 2020

Make sure your MongoDB connection string is set in the environment variable the right way

MONGO_URI=mongodb://localhost:27017/e-wallet-app - use this if you have MongoDB installed and what to run locally or you can use
MONGO_URI=mongodb://mongo:27017/e-wallet-app if have configured docker installed and you want to run the app image

Or perhaps a MongoDB Altas connection string...

@amoungui
Copy link
Author

amoungui commented Apr 22, 2020 via email

@Ethical-Ralph
Copy link

That a long process but send me an email at [email protected] im willing to put you through

@amoungui
Copy link
Author

amoungui commented Apr 23, 2020 via email

@Ethical-Ralph
Copy link

Note I didn’t write this API. You’re just fortunate I’m here and decided to help

@amoungui
Copy link
Author

amoungui commented Apr 23, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants