使用 Node 和 MongoDB 建立登錄檔單


在本文中,我們將建立一個包含一些引數的簡單使用者登錄檔單。點選“儲存”後,所有使用者詳細資訊將儲存到 MongoDB 資料庫中。

安裝

在開始建立登錄檔單之前,必須在您的系統上成功安裝以下依賴項。

  • 使用以下命令檢查並安裝 express。Express 用於設定中介軟體以響應 HTTP 請求。

npm install express --save
  • 設定“body-parser”節點模組以讀取 HTTP POST 資料。

npm install body-parser --save
  • 設定“mongoose”,因為它位於 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://: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

<!DOCTYPE html>
<html>
<head>
<title> Signup Form</title>
<link rel="stylesheet"
href=
"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
integrity=
"sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<br>
<br>
<br>
<div class="container" >
<div class="row">
</div>
<div class="main">
   <form action="/sign_up" method="post">
   <h1>Welcome to Tutorials Point - SignUp</h1>
   <input class="box" type="text" name="name" id="name" placeholder="Name" required /><br>
   <input class="box" type="email" name="email" id="email" placeholder="E-Mail " required /><br>
   <input class="box" type="password" name="password" id="password" placeholder="Password " required/><br>
   <input class="box" type="text" name="phone" id="phone" placeholder="Phone Number " required/><br>
   <br>
   <input type="submit" id="submitDetails" name="submitDetails" class="registerbtn" value="Submit" />   <br>
   </form>
</div>
<div class="">
</div>
</div>
</div>
</body>
</html>

success.html

<!DOCTYPE html>
<html>
<head>
<title> Signup Form</title>
<link rel="stylesheet"
href=
"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
integrity=
"sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<br>
<br>
<br>
<div class="container" >
   <div class="row">
   <div class="col-md-3">
</div>
<div class="col-md-6 main">
   <h1> Signup Successful</h1>
</div>
<div class="col-md-3">
</div>
</div>
</div>
</body>
</html>

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;
}

輸出

現在,在您的網路瀏覽器上嘗試此連結。您將看到一個註冊頁面。

http://127.0.0.1:3000/index.html 或 https://: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

  

註冊頁面

   

成功頁面

 

 記錄已成功插入到 mongoDB 中

 

更新於: 2021年4月29日

4K+ 次瀏覽

開啟您的 職業生涯

完成課程後獲得認證

立即開始
廣告