【发布时间】:2019-10-06 12:01:19
【问题描述】:
我无法弄清楚如何在 mongodb 中检索数据并将其提取到 html/ejs 文件中。在 html/ejs 文件中有一个按钮,如果用户单击它,它将显示数据库集合 mongodb 中的所有数据。
我发现了一些与我的问题类似的问题,但它没有回答我的问题。我还是 node js 和 mongodb 的新手,所以我真的不知道如何实现我的目标。
这是我的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);
});
一旦我用一个按钮创建了 ejs 文件,我需要在一个表中显示所有数据。谢谢!
【问题讨论】:
标签: javascript node.js mongodb ejs