Click here to Skip to main content
15,851,387 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
const express = require("express");
const handlebars = require("express-handlebars");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const path = require("path");

const app = express();
const port = 3000;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.engine(".hbs", handlebars({ defaultLayout: "main", extname: ".hbs" }));
app.set("view engine", ".hbs");
app.use(express.static(path.join(__dirname, "public")));
app.use(express.urlencoded({ extended: false }));
app.use(express.json());

mongoose.Promise = global.Promise;
mongoose.connect(
"mongodb://usresa:passcode@mongodb-rukshi-shard-00-00.nerbj.gcp.mongodb.net:27017,mongodb-rukshi-shard-00-01.nerbj.gcp.mongodb.net:27017,mongodb-rukshi-shard-00-02.nerbj.gcp.mongodb.net:27017/db_name?ssl=true&replicaSet=atlas-dxrzem-shard-0&authSource=admin&retryWrites=true&w=majority",
{ useNewUrlParser: true, useUnifiedTopology: true }
);
const nameSchema = new mongoose.Schema({
name: String,
naquantityme: String,
description: String,
});
const User = mongoose.model("User", nameSchema);

// app.get("/", (req, res) => {
// res.sendFile(__dirname + "/AddProduct");
// });

app.get("/", (req, res) => {
res.render("login", { layout: "loginlayout" });
});

app.get("/home", (req, res) => {
res.render("dashboard", { layout: "main" });
});

app.use("/AddProduct", (req, res) => {
res.render("AddProduct", { layout: "main" });
});

app.post("/addproductform", (req, res) => {
var myData = new User(req.body);
myData
.save()
.then((item) => {
res.send("Product saved to database");
})
.catch((err) => {
res.status(400).send("Unable to save to database");
});
});

app.listen(port, () => {
console.log("Server listening on port " + port);
});

What I have tried:

Tried to relocalete hbs pages from views to outside view folder in project but its not working.
Posted
Updated 29-Jul-20 21:53pm
v2

1 solution

An example of insert:
JavaScript
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  var dbo = db.db("mydb");
  var myobj = { name: "Company Inc", address: "Highway 37" };
  dbo.collection("customers").insertOne(myobj, function(err, res) {
    if (err) throw err;
    console.log("1 document inserted");
    db.close();
  });
});

There is a good documentation on connecting MongoDB using NodeJS, did you have a look at it:
Connect to a MongoDB Database Using Node.js | MongoDB Blog[^]
Node.js MongoDB Get Started[^]
https://docs.mongodb.com/drivers/node/[^]
 
Share this answer
 
Comments
Sam_k_khan 30-Jul-20 3:00am    
This is direct connection to mongo. How about on button submit of view? On Button Click.
as shown in above code "/addproductform"
Sandeep Mewara 30-Jul-20 3:02am    
https://bl.ocks.org/aerrity/fd393e5511106420fba0c9602cc05d35

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900