Use node functions in my html
Create a route to your getSongById()
function in node and then, from your html file make an ajax request to that url.
Let's say, in your app.js, have something like:
app.get('/random-video', function(req, res) { console.log('random video method called'); var numberOfSongs = db.getNextFreeId()-1; idToGet=Math.floor(Math.random() * numberOfSongs); db.getSongById(idToGet, function(err, song){ console.log('found random song: '+ song); res.send(JSON.stringify(song)); }); });
You'll also have to modify your getSongById()
function to be async, like:
database.prototype.getSongById=function(id2, cb){ songModel.findOne({id : id2}, function(err,obj){ if (err) { console.log('Not Found, error: '+err); cb(err); } else if (obj) { console.log('Found:', obj); cb(null, obj); } });};
Then, in your html page, once you have jQuery loaded, do something like:
<script>$(document).ready(function(){ $.ajax({ url: '/random-video', contentType: 'application/json; charset=utf-8', }).done(function(song){ //do stuff with the song });})</script>