MongoDB connection error: Error: slash in host identifier
Opened this issue · 6 comments
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
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...
That a long process but send me an email at rakinola90@gmail.com im willing to put you through
Note I didn’t write this API. You’re just fortunate I’m here and decided to help