I’m new to node.js and MongoDB. Installed all packages as says in a YouTube tutorial. I tried a lot to connect **MongoDB **with node.js. but, there is no error messages and not connected. I have downloaded **Mongo Shell ** also. everything works perfectly, the connection message is not displaying. It works on my command prompt. But not in vs code. I’m creating a sample project with express generator. What’s wrong with my code?
Here is my code…
var express = require('express');
var router = express.Router();
var MongoClient = require('mongodb').MongoClient
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
router.post('/signup', function(req, res) {
console.log(req.body);
MongoClient.connect('mongodb://localhost:27017', function(err, client){
if(err)
console.log('error')
else
console.log('mongodb connected...')
})
res.send('Got it.')
})
2
Answers
Try my simple code below,
If work it says "Connected successfully to server" in the console log
It is only the first step…