nodejs操作數(shù)據(jù)庫mongodb

全部代碼:
html代碼:

<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   <title>viewOne</title>
   <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
   <meta name="apple-mobile-web-app-capable" content="yes">
   <meta name="apple-mobile-web-app-status-bar-style" content="black">
   <meta name="format-detection" content="telephone=no" />
   <link rel="stylesheet" type="text/css" href="../public/style.css" />
</head>
<body>
<div class="content">
   <header class="common-header">
       <a class="icon-back" href="javascript:history.go(-1);"></a>
       <span>viewTwo取消</span>
   </header>
   <div>
       <form action="http://192.168.10.57:4000/regist" method="get">
           <input type="text" name="first_name" />
           <br>
           <input type="text" name="last_name"/>
           <input type="submit" value="提交">
       </form>
   </div>
</div>
</body>
<script type="text/javascript">
   var adaptPageClass = 'content';
</script>
</html>

后臺代碼:

var MongoClient = require('mongoose');
MongoClient.connect('mongodb://localhost:27017/node數(shù)據(jù)庫',{ useNewUrlParser: true },function () {  //27017是數(shù)據(jù)庫端口,node數(shù)據(jù)庫是數(shù)據(jù)庫名
    console.log('test')
});
//提示數(shù)據(jù)庫是否鏈接成功
var db = MongoClient.connection;
db.on('open',function () {
    console.log('MongoDB鏈接成功')
})
db.on('error',function () {
    console.log('MongoDB連接失敗');
})
var Schema = MongoClient.Schema; //創(chuàng)建一個Schema模型骨架,并且設置好user模版
var userSchema = new Schema({
    firstName:{type:String},
    lastName:{type:String}
});
var User = MongoClient.model("User",userSchema);  //Schema發(fā)布一個User的Model
app.get('/regist',function (req,res) {  處理regist的get請求
    console.log('輕取i成功')
    console.log(req.query.first_name)
    var user_1 = new User({
        firstName: req.query.first_name,
        lastName: req.query.last_name
    });
    user_1.save(function () {
        res.send('數(shù)據(jù)插入成功');  //插入數(shù)據(jù)并且提示數(shù)據(jù)庫插入成功
    });
});

1、鏈接數(shù)據(jù)庫:

var MongoClient = require('mongoose');
MongoClient.connect('mongodb://localhost:27017/node數(shù)據(jù)庫',{ useNewUrlParser: true },function () {  //27017是數(shù)據(jù)庫端口,node數(shù)據(jù)庫是數(shù)據(jù)庫名
    console.log('test')
});

//提示數(shù)據(jù)庫是否鏈接成功
var db = MongoClient.connection;
db.on('open',function () {
    console.log('MongoDB鏈接成功')
})
db.on('error',function () {
    console.log('MongoDB連接失敗');
})

2、創(chuàng)建一個Schema骨架模型,并且設計好user模板

var Schema = MongoClient.Schema; //創(chuàng)建一個Schema模型骨架,并且設置好user模版
var userSchema = new Schema({
    firstName:{type:String},
    lastName:{type:String}
});

3、發(fā)布模型

var User = MongoClient.model("User",userSchema);  //Schema發(fā)布一個User的Model

4、處理前端請求

app.get('/regist',function (req,res) {  處理regist的get請求
    console.log('輕取i成功')
    console.log(req.query.first_name)
    var user_1 = new User({
        firstName: req.query.first_name,
        lastName: req.query.last_name
    });
    user_1.save(function () {
        res.send('數(shù)據(jù)插入成功');  //插入數(shù)據(jù)并且提示數(shù)據(jù)庫插入成功
    });
});

借鑒自:https://blog.csdn.net/qwe502763576/article/details/79646551
http://vince.xin/article/5acc33286b78214ab8ac58b6

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 全部代碼:html代碼: 后臺代碼: 1、鏈接數(shù)據(jù)庫: 2、創(chuàng)建一個Schema骨架模型,并且設計好user模板 ...
    天字一等閱讀 364評論 0 0
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,319評論 25 708
  • 用兩張圖告訴你,為什么你的 App 會卡頓? - Android - 掘金 Cover 有什么料? 從這篇文章中你...
    hw1212閱讀 14,096評論 2 59
  • 上上周,我在朋友圈看到環(huán)黃山100KM騎行的報名帖,還沒有去過黃山的我立刻有點小興奮,把鏈接丟在了跑馬群里。立刻有...
    肖爺_族長閱讀 6,157評論 23 11
  • 這周我回到了運城,開始補作業(yè)和沖擊閱讀量。我想在暑假剩下的日子里更加努力。 我看了一些蛋糕和香腸類書,覺...
    橄欖樹翔閱讀 464評論 1 1

友情鏈接更多精彩內容