Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> MongoDB

Node और MongoDB का उपयोग करके साइन-अप फॉर्म बनाएं


इस लेख में, हम कुछ पैरामीटर्स के साथ एक सरल यूज़र साइन-अप फॉर्म बनाएंगे। SAVE पर क्लिक करने पर, सभी यूज़र विवरण MongoDB डेटाबेस में सहेजे जाएंगे।

इंस्टॉलेशन

साइन-अप फॉर्म बनाने से पहले, निम्नलिखित निर्भरताओं को आपके सिस्टम पर सफलतापूर्वक इंस्टॉल किया जाना चाहिए।

  • निम्न कमांड का उपयोग करके एक्सप्रेस की जांच करें और इंस्टॉल करें। एक्सप्रेस का उपयोग HTTP अनुरोधों का जवाब देने के लिए मिडलवेयर सेट करने के लिए किया जाता है।

npm install express --save
  • HTTP POST डेटा पढ़ने के लिए "बॉडी-पार्सर" नोड मॉड्यूल सेटअप करें।

npm install body-parser --save
  • "मोंगूज़" सेटअप करें, क्योंकि यह Node के MongoDB ड्राइवर के ऊपर बैठता है।

npm install mongoose --save

उदाहरण 1

  • निम्नलिखित फाइलें बनाएं और प्रत्येक फाइल के लिए नीचे दिए गए कोड स्निपेट को कॉपी-पेस्ट करें:

    • app.js

    • public (एक नया फोल्डर बनाएं और नीचे दी गई फाइलें इसमें पेस्ट करें।)

      • index.html

      • success.html

      • style.css

  • अब, एप्लिकेशन चलाने के लिए निम्न कमांड चलाएं।

node app.js

कोड स्निपेट्स

app.js

var express=require("express"); var bodyParser=require("body-parser"); const mongoose = require('mongoose'); mongoose.connect('mongodb://localhost:27017/tutorialsPoint'); var db=mongoose.connection; db.on('error', console.log.bind(console, "connection error")); db.once('open', function(callback){ console.log("connection succeeded"); }) var app=express() app.use(bodyParser.json()); app.use(express.static('public')); app.use(bodyParser.urlencoded({ extended: true })); app.post('/sign_up', function(req,res){ var name = req.body.name; var email =req.body.email; var pass = req.body.password; var phone =req.body.phone; var data = { "name": name, "email":email, "password":pass, "phone":phone } db.collection('details').insertOne(data,function(err, collection){ if (err) throw err; console.log("Record inserted Successfully"); }); return res.redirect('success.html'); }) app.get('/',function(req,res){ res.set({ 'Access-control-Allow-Origin': '*' }); return res.redirect('index.html'); }).listen(3000) console.log("server listening at port 3000");

index.html

    Signup Form     


Welcome to Tutorials Point - SignUp







success.html

    Signup Form     


Signup Successful

style.css

.main{ padding:20px; font-family: 'Helvetica', serif; box-shadow: 5px 5px 7px 5px #888888; } .main h1{ font-size: 40px; text-align:center; font-family: 'Helvetica', serif; } input{ font-family: 'Helvetica', serif; width: 100%; font-size: 20px; padding: 12px 20px; margin: 8px 0; border: none; border-bottom: 2px solid #4CAF50; } input[type=submit] { font-family: 'Helvetica', serif; width: 100%; background-color: #4CAF50; border: none; color: white; padding: 16px 32px; margin: 4px 2px; border-radius: 10px; } .registerbtn { background-color: #4CAF50; color: white; padding: 16px 20px; margin: 8px 0; border: none; cursor: pointer; width: 100%; opacity: 0.9; }

आउटपुट

अब इस लिंक को अपने वेब ब्राउज़र पर आज़माएं। आपको एक रजिस्ट्रेशन पेज दिखाई देगा।

https://127.0.0.1:3000/index.html OR https://localhost:3000/index.html

C:\Users\tutorialsPoint\> node app.js server listening at port 3000 (node:73542) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect. (node:73542) [MONGODB DRIVER] Warning: Current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor. connection succeeded

साइन-अप पेज

Node और MongoDB का उपयोग करके साइन-अप फॉर्म बनाएं

सक्सेस पेज

Node और MongoDB का उपयोग करके साइन-अप फॉर्म बनाएं

रिकॉर्ड MongoDB में सफलतापूर्वक डाला गया

Node और MongoDB का उपयोग करके साइन-अप फॉर्म बनाएं


  1. - MongoDB updateOne() और insertOne() का उपयोग

    MongoDB insertOne() एक डॉक्यूमेंट को कलेक्शन में डालता है, जबकि updateOne() क्वेरी फ़िल्टर के आधार पर कलेक्शन में एक डॉक्यूमेंट को अपडेट करता है। आइए डॉक्यूमेंट्स के साथ एक कलेक्शन बनाते हैं: > db.demo735.insertOne({id:1,Name:Chris}); { acknowledged : true, insertedId : ObjectId(5ead51b657bb72a1

  1. - MongoDB और Python

    MongoDB एक व्यापक रूप से उपयोग किया जाने वाला डॉक्यूमेंट डेटाबेस है जो NoSQL DB का एक रूप भी है। Python कुछ पायथन मॉड्यूल्स के माध्यम से MongoDB के साथ इंटरैक्ट कर सकता है और Mongo DB के अंदर डेटा बना और मैनिपुलेट कर सकता है। इस लेख में हम इसे करना सीखेंगे। लेकिन MongoDB आपके सिस्टम में पहले से उपल

  1. - MongoDB एग्रीगेशन और प्रोजेक्शन

    इसके लिए aggregate() के साथ $project का उपयोग करें। एग्रीगेशन में $project पाइपलाइन में अगले चरण के लिए अनुरोधित फील्ड्स के साथ डॉक्यूमेंट्स को पास करता है। आइए एक कलेक्शन बनाते हैं: > db.demo762.insertOne({ _id : { userId:101, userName:Chris }, countryName : US, details : [ { Name : Robert, Due