I had a relatively old clientside javascript/html/css project that I want to hook up to a MongoDB server. I already set up the express folder so that the clientside is running correctly. However, I have no idea how to access the DB from clientside(which is where I'll need to extract the data initially). I also don't know how to connect the nodejs files(like from /router and app.js) to the javascript files in the clientside. It feels to me that the clientside is separated from MongoDB/nodejs and I'm sure there's a way to do it - I've searched but there were no useful results. Any idea on how to do this?
Ex.
router.get('/main.html', function(req, res) {
res.render('main', { title: 'Express' });
var db = req.db;
var username = localStorage.getItem('company');//contains the info to check if there is an overlap.
var password = localStorage.getItem('key');
var collection = db.get('usercollection');//gets the specific collection group
//if(wantUpdate){
collection.remove( [{"username" : username} , {"password" : password}] );
//}
//else{
//}
if(collection.find( [{"username" : username} , {"password" : password}]).count() == 0)//means there is no need to add an entry
{
collection.insert( [{"username": username} , {"password" : password}, {"jsonMain" : /*HOW DO I FIND THIS*/}, {"jsonMilestones" : /*HOW DO I FIND THIS*/}, {"jsonTasks" : /*HOW DO I FIND THIS*/}]);
}
});
note: Not good with this code block thing - I swear I indent properly in my programs!
You can't access directly mongoDB from client-side for obvious reasons. You'd want to make an http request from your client side to your server (or check also socket.io, it's easy and convenient), then query the database on server side and respond to the request sending the data you needed.