save data in database and Read all data part-2
const express = require('express')
const app = express()
const port = 3000
const bodyParser = require('body-parser')
const { MongoClient, ServerApiVersion } = require('mongodb');
const uri = "mongodb+srv://abcmehedi5:WD1kd4J7lElqvmJN@cluster0.mbjz2.mongodb.net/?retryWrites=true&w=majority";
// const password = 'WD1kd4J7lElqvmJN'
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false })) //for use in the form
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html')
})
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
client.connect(err => {
const productcollection = client.db("organicdb").collection("products");
app.post('/addProduct', (req , res) =>{
const product= req.body
productcollection.insertOne(product)
.then(result => {
console.log(" one product added sucessfull")
})
})
// client.close();
});
app.listen(port, () => {
console.log("listen working")
})
html file............................................
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>This is node js node js mongo bd project</h1>
<form action="/addProduct" method="post">
<input type="text" placeholder="Name" name="name" id="">
<input type="text" placeholder="Price" name="price" id="">
<input type="text" placeholder="Quantity" name="quantity" id="">
<button type="submit">Add Product</button>
</form>
</body>
</html>
No comments