I'm having trouble figuring out how can I retrieve data in mongodb and fetch it in an html/ejs file. in html/ejs file there is one button where if the user click it, it will display all data in database collection mongodb.
I found some questions similar to my question but it doesn't answer my question. I am still new at node js and mongodb so I don't really have an Idea on how can I achieve my goal.
this is my index.js
var express = require("express");
var app = express();
app.set('view engine', 'ejs')
//var hostname = '127.0.0.1';
var port = 3000;
var mongoose = require("mongoose");
app.set('view engine','jade');
mongoose.Promise = global.Promise;
mongoose.connect("mongodb://localhost:27017/commuters", {useNewUrlParser: true});
app.use('/gui', express.static('gui'));
//use to link static file in the folder named public
var nameSchema = new mongoose.Schema({
route : String,
origin : String,
destination : String,
estimatedTimeOfArrival : String,
date : String,
time : String
},
{
collection : 'boardingAlight'
});
//collection is the name of collection that you created in the same database name above
var User = mongoose.model("User", nameSchema);
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true, useNewUrlParser : true }));
app.use("/", (req, res) => {
res.sendFile(__dirname + "/gui/index.html");
});
//FOR PORT CONNECTION
//localhost:3000
app.listen(port, () => {
console.log("Server listening on port " + port);
});
once I created ejs file with a button, I need to display the all the data in a table. Thank you!