0
I'm Following a MERN Stack tutorial from Udemy and I'm trying to connect mongodb with mongoose. The code I have configures is here:
(db.js)
const mongoose = require('mongoose');
const config = require('config');
const db = config.get('mongoURI');
const connectDB = async () => {
try {
await mongoose.connect(db);
console.log('MongoDB Connect...');
} catch (err) {
console.error(err.message);
//Exit process with falure
process.exit(1);
}
};
module.exports = connectDB;
(server.js)
const express = require('express');
const connectDB = require('./config/db');
const app = express();
//connect database
connectDB();
app.get('/', (req, res) => res.send('API Running'));
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Server started on port ${PORT}`));
(package.json)
{
"name": "devconnect",
"version": "1.0.0",
"description": "Social Network for Developers",
"main": "server.js",
"scripts": {
"start": "node server",
"server": "nodemon server"
},
"author": "Jason Thomas",
"license": "MIT",
"dependencies": {
"bcryptjs": "^2.4.3",
"config": "^3.2.5",
"express": "^4.17.1",
"express-validator": "^6.3.1",
"gravatar": "^1.8.0",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.8.9",
"request": "^2.88.0"
},
"devDependencies": {
"concurrently": "^5.0.2",
"nodemon": "^2.0.2"
}
}
After I enter [npm run server]This is my error message
Server started on port 5000
Password contains an illegal unescaped character
[nodemon] app crashed - waiting for file changes before starting...
I cant figure this out and I know its something simple, What am I doing wrong, How do I fix this?
Best Regards & Thank You
If you have the same folder structure, you should have a config/default.json like this, containing the string you copy-pasted from your mongoDB cluster connection settings:
{
"mongoURI": "mongodb+srv://username:<password>@devconnector-clusterid.mongodb.net/somethingSomething?retryWrites=true",
"jwtSecret": "secret",
"githubClientId": "",
"githubSecret": ""
}
Be sure to remove the characters < > when you enter your password.
All the files for the new course here - Devconnector 2.0 In the older version of the course it's config/keys.js .