//userModel.js
var mongoose = require("mongoose");
var fakerSchema = new mongoose.Schema({
product: String,
productname: String,
productadjective: String,
productMaterial: String,
productcommerce: String,
});
module.exports = mongoose.model("fak", fakerSchema);
//views= home.ejs
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
</head>
<body>
<form action="/" method="post">
<input type="submit" value="click to insert the data" />
</form>
<br />
<%if(data.length>0){%>
<table border="1" style="text-align: center">
<thead>
<tr>
<th>s.no</th>
<th>product</th>
<th>productname</th>
<th>productadjective</th>
<th>productMaterial</th>
<th>productcommerce</th>
</tr>
</thead>
<tbody>
<%for(var i=1;i< data.length; i++){%>
<tr>
<td><%= i%></td>
<td><%= data[i].product%></td>
<td><%= data[i].productname%></td>
<td><%= data[i].productadjective%></td>
<td><%= data[i].productMaterial%></td>
<td><%= data[i].productcommerce%></td>
</tr>
<%}%>
</tbody>
</table>
<%}%>
</body>
</html>
//app.js
var express = require("express");
var mongoose = require("mongoose");
var faker = require("faker");
var path = require("path");
var fakerModel = require("./models/user");
mongoose
.connect(
"mongodb+srv://Sukh99:mongodb@cluster0.qeqp1.mongodb.net/FakeDatas?retryWrites=true&w=majority",
{ useNewUrlParser: true }
)
.then(() => console.log("connected to db"))
.catch((error) => console.log("connection error", error));
var app = express();
// path in html
app.set("view engine", "ejs");
app.set("views", path.resolve(__dirname, "views"));
app.get("/", (req, res) => {
fakerModel.find((err, data) => {
// console.log("data", data);
if (err) {
console.log(err);
} else if (data) {
res.render("home", { data: data });
} else {
res.render("home", { data: {} });
}
});
});
app.post("/", (req, res) => {
for (var i = 0; i <= 10; i++) {
var fakee = new fakerModel({
product: faker.commerce.product(),
productname: faker.commerce.productName(),
productadjective: faker.commerce.productAdjective(),
productMaterial: faker.commerce.productMaterial(),
productcommerce: faker.commerce.price(100, 200, 0, "$"),
});
// console.log("fakee", fakee);
fakee.save((err, data) => {
if (err) {
console.log(err);
}
});
}
res.redirect("/");
});
var port = process.env.PORT || 3000;
app.listen(port, () => console.log("server running at port " + port));
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter