I am trying to connect mongodb to my express nodejs web application. I am fresh new to nodejs. I am following this tutorial video https://www.youtube.com/watch?v=eB9Fq9I5ocs but I couldn't complete it due to the connection of mongodb.
the app.js code I have:
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
Genre = require('./models/genre');
mongoose.connect('mongodb://localhost/bookstore');
mongoose.connection.on('error', (err) => {
console.error('mongodb connection error', err);
});
mongoose.connection.on('connected', () => {
console.info(`Connected to mongodb`);
});
mongoose.connection.on('disconnected', () => {
console.info('Disconnected from mongodb');
});
// var mongoose = connect('mongodb://localhost/bookstore');
// var db = mongoose.connection;
app.get('/', function(req, res){
res.send('Hello World');
});
app.get('api/genres', function(req , res){
Genre.getGenres(function(err, genres){
if(err){
throw err;
}
res.json(genres);
})
});
app.listen(3666);
console.log('Server Running On http://localhost:3666');
and this is the genre.js
var mongoose = require('mongoose');
var genreSchema = mongoose.Schema({
name:{
type: String,
requires: true
},
create_date:{
type: Date,
default: Date.now
}
});
var Genre = module.exports = mongoose.model('Genre', genreSchema);
module.exports.getGenres = function(callback, limit){
Genre.find(callback).limit(limit);
}
and this is a picture of the database in the terminal
I know this is a basic question but I couldnt figured out I check on google there are others way to connect to the database but I need to know why this particular way which I just followed from the tutorial video havent worked. As you noticed I am a new to nodejs web development so if you could suggest websites or youtube channels to get me start it I would appreciate it.

