欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

從零開始學(xué)習(xí)Node.js系列教程之SQLite3和MongoDB用法分析

 更新時間:2017年04月13日 14:43:38   作者:MIN飛翔  
這篇文章主要介紹了Node.js SQLite3和MongoDB用法,結(jié)合實(shí)例形式分析了SQLite3和MongoDB數(shù)據(jù)庫的初始化、連接、查詢等操作的實(shí)現(xiàn)技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下

本文實(shí)例講述了Node.js中SQLite3和MongoDB的用法。分享給大家供大家參考,具體如下:

setup.js:初始化數(shù)據(jù)庫

var util = require('util');
var async = require('async');  //npm install async
var notesdb = require('./nodesdb-sqlite3');
// var notesdb = require('./notesdb-mongoose');
notesdb.connect(function(error){
  if (error) throw error;
});
notesdb.setup(function(error){
  if (error){
    util.log('ERROR ' + error);
    throw error;
  }
  async.series([  //async.series函數(shù)可以控制函數(shù)按順序執(zhí)行,從而保證最后的函數(shù)在所有其他函數(shù)完成之后執(zhí)行
    function(cb){
      notesdb.add("test", "testtest",
      function(error){
        if (error) util.log('ERROR ' + error);
        cb(error);
      });
    }
  ],
    function(error, results){
      if (error) util.log('ERROR ' + error);
      notesdb.disconnect(function(err){});
    }
  );
});

nodesdb-sqlite3.js

SQLite3 是一個輕量級的進(jìn)程內(nèi)SQL引擎

它是一個無服務(wù)器且無需配置的SQL數(shù)據(jù)庫引擎,僅僅是作為一個獨(dú)立的庫被鏈接到應(yīng)用程序上

npm install sqlite3 安裝此模塊之前先在系統(tǒng)上安裝sqlite3庫 http://www.sqlite.org/download.html 下載

//數(shù)據(jù)庫接口庫
var util = require('util');
var sqlite3 = require('sqlite3');
sqlite3.verbose();
var db = undefined;
/*
 數(shù)據(jù)庫名是直接硬編碼的,所以當(dāng)調(diào)用connect和setup函數(shù)時,當(dāng)前目錄中就會生成chap06.sqlite3文件
 */
exports.connect = function(callback){
  db = new sqlite3.Database("chap06.sqlite3", sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE,
    function(err){
      if (err){
        util.log('FAIL on creating database ' + err);
        callback(err);
      } else {
        callback(null);
      }
    });
}
//此處的disconnect函數(shù)是空的
exports.disconnect = function(callback){
  callback(null);
}
exports.setup = function(callback){
  db.run("CREATE TABLE IF NOT EXISTS notes " +
    "(ts DATETIME, author VARCHAR(255), note TEXT)",
    function(err){
      if (err){
        util.log('FAIL on creating table ' + err);
        callback(err);
      } else {
        callback(null);
      }
    });
}
exports.emptyNote = {"ts": "", author: "", note: ""};
exports.add = function(author, note, callback){
  db.run("INSERT INTO notes (ts, author, note) " +
    "VALUES (?, ?, ?);",
    [new Date(), author, note],
    function(error){
      if (error){
        util.log('FAIL on add ' + error);
        callback(error);
      } else {
        callback(null);
      }
    });
}
/*
run函數(shù)接受一個字符串參數(shù),其中?表示占位符,占位符的值必須通過一個數(shù)組傳遞進(jìn)來
調(diào)用者提供了一個回調(diào)函數(shù),然后通過這個回調(diào)函數(shù)來聲明錯誤
 */
exports.delete = function(ts, callback){
  db.run("DELETE FROM notes WHERE ts = ?;",
    [ts],
    function(err){
      if (err){
        util.log('FAIL to delete ' + err);
        callback(err);
      } else {
        callback(null);
      }
    });
}
exports.edit = function(ts, author, note, callback){
  db.run("UPDATE notes " +
    "SET ts = ?, author = ?, note = ? " +
    "WHERE ts = ?",
    [ts, author, note, ts],
    function(err){
      if (err){
        util.log('FAIL on updating table ' + err);
        callback(err);
      } else {
        callback(null);
      }
    });
}
exports.allNotes = function(callback){
  util.log(' in allnote');
  db.all("SELECT * FROM notes", callback);
}
exports.forAll = function(doEach, done){
  db.each("SELECT * FROM notes", function(err, row){
    if (err){
      util.log('FAIL to retrieve row ' + err);
      done(err, null);
    } else {
      doEach(null, row);
    }
  }, done);
}
/*
allNotes和forAll函數(shù)是操作所有數(shù)據(jù)的兩種方法,allNotes把數(shù)據(jù)庫中所有的數(shù)據(jù)行收集到一個數(shù)組里,
而forAll方法可以接受兩個回調(diào)函數(shù),每當(dāng)從數(shù)據(jù)集中拿一行數(shù)據(jù),回調(diào)函數(shù)doEach都會執(zhí)行一遍,當(dāng)讀完所有數(shù)據(jù)時,回調(diào)函數(shù)done就會執(zhí)行
 */
exports.findNoteById = function(ts, callback){
  var didOne = false;
  db.each("SELECT * FROM notes WHERE ts = ?",
    [ts],
    function(err, row){
      if (err){
        util.log('FAIL to retrieve row ' + err);
        callback(err, null);
      } else {
        if (!didOne){
          callback(null, row);
          didOne = true;  //保證回調(diào)函數(shù)只被執(zhí)行一次
        }
      }
    });
}

notesdb-mongoose.js

MongoDB是nosql數(shù)據(jù)庫的領(lǐng)頭羊之一,"可擴(kuò)展、高性能、開源、面向文檔的數(shù)據(jù)庫",它使用JSON風(fēng)格的文檔。

Mongoose是用于訪問MongoDB的模塊之一,它是一個對象建模工具,意味著你的程序負(fù)責(zé)定義模式對象來描述數(shù)據(jù),

而Mongoose負(fù)責(zé)數(shù)據(jù)到MongoDB的存儲。

Mongoose對于Node和MongoDB而言是一個非常強(qiáng)大的對象建模工具,使用嵌入式文檔,是一個類型靈活的系統(tǒng),

適用于字段輸入、字段驗(yàn)證、虛擬字段等。

MongoDB在Windows下安裝部署 :http://www.dbjr.com.cn/article/111112.htm

安裝Mongoose模塊

npm install mongoose

Mongoose不是唯一一個在node中使用MongoDB的工具。

var util = require('util');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var dburl = 'mongodb://localhost/chap06'; //dburl用于連接已運(yùn)行的MongoDB
exports.connect = function(callback){
  mongoose.connect(dburl);
}
exports.disconnect = function(callback){
  mongoose.disconnect(callback);
}
exports.setup = function(callback){callback(null);}
//定義模式
var NoteSchema = new Schema({
  ts: {type: Date, default: Date.now}, //默認(rèn)值
  author: String,
  note: String
});
//將NoteSchema作為Mongoose的模型注冊進(jìn)去
mongoose.model('Note', NoteSchema);
var Note = mongoose.model('Note');
exports.emptyNote = {"_id": "", author: "", note: ""};
exports.add = function(author, note, callback){
  var newNote = new Note();
  newNote.author = author;
  newNote.note = note;
  newNote.save(function(err){
    if (err){
      util.log('FATAL ' + err);
      callback(err);
    } else {
      callback(null);
    }
  });
}
exports.delete = function(id, callback){
  exports.findNoteById(id, function(err, doc){
    if (err){
      callback(err);
    } else {
      util.log(util.inspect(doc));
      doc.remove();
      callback(null);
    }
  });
}
exports.edit = function(id, author, note, callback){
  exports.findNoteById(id, function(err, doc){
    if (err){
      callback(err);
    } else {
      doc.ts = new Date();
      doc.author = author;
      doc.note = note;
      doc.save(function(err){
        if (err){
          util.log('FATAL ' + err);
          callback(err);
        } else {
          callback(null);
        }
      });
    }
  });
}
exports.allNotes = function(callback){
  Note.find({}, callback);
}
exports.forAll = function(doEach, done){
  Note.find({}, function(err, docs){
    if (err){
      util.log('FATAL ' + err);
      done(err, null);
    }
    docs.forEach(function(doc){
      doEach(null, doc);
    });
    done(null);
  });
}
/*
_id字段是MongoDB提供的全局唯一的ID,用于標(biāo)識存儲的文檔
 */
var findNoteById = exports.findNoteById = function(id, callback){
  Note.findOne({_id: id}, function(err, doc){
    if (err){
      util.log('FATAL ' + err);
      callback(err, null);
    }
    callback(null, doc);
  });
}

app.js

//在數(shù)據(jù)庫需要放置在一臺計算機(jī)上時,應(yīng)該考慮使用SQLite3
//控制器,在nodesdb-sqlite3.js和notesdb-mongoose.js模塊之間切換
var util = require('util');
var url = require('url');
var express = require('express');
var nmDbEngine = 'sqlite3'; //用于命名數(shù)據(jù)庫引擎、選擇合適的notesdb實(shí)現(xiàn)和選擇合適的views目錄
//var nmDbEngine = 'mongoose';
var notesdb = require('./nodesdb-' + nmDbEngine);
var app = express();
app.use(express.logger());
app.use(express.cookieParser()); //添加cookieParser中間件
app.use(express.bodyParser());
app.engine('.html', require('ejs').__express);  //3.X
//app.register('.html', require('ejs'));  //2.X
app.set('views', __dirname + '/views-' + nmDbEngine);
app.set('view engine', 'ejs');
//是一個路由中間件函數(shù),用于在一些路由器函數(shù)中解析URL查詢參數(shù)
var parseUrlParams = function(req, res, next){
  req.urlP = url.parse(req.url, true);
  next();
}
//檢查用戶是否被允許訪問,這里只檢查cookie是否等于AOK,這個單詞通常意味著一切都沒問題
/*
 很多應(yīng)用都需要用戶登錄,然后用戶才能進(jìn)行一些特權(quán)操作。由于HTTP是一個無狀態(tài)的協(xié)議,
 驗(yàn)證用戶的唯一方式就是發(fā)送一個cookie到瀏覽器上,然后驗(yàn)證標(biāo)識符。cookie包含了應(yīng)用中用于驗(yàn)證用戶的數(shù)據(jù)。
 cookieParser中間件在這里做了很多工作,查找cookie,解析cookie,然后將解析出來的值讓到req對象中。
 當(dāng)存在cookie時,它的值會被放入req.cookies中。
 */
var checkAccess = function(req, res, next){
  if (!req.cookies || !req.cookies.notesaccess || req.cookies.notesaccess !== "AOK"){
    res.redirect('/login');
  } else {
    next();
  }
}
notesdb.connect(function(error){
  if (error) throw error;
})
app.on('close', function(error){
  notesdb.disconnect(function(err){});
});
app.get('/', function(req, res) {res.redirect('/view');});
app.get('/view', checkAccess, function(req, res){ //可以在每個路由上加checkAccess檢查
  notesdb.allNotes(function(err, notes){
    if (err){
      util.log('ERROR ' + err);
      throw err;
    } else {
      res.render('viewnotes.html', {title: "Notes ("+ nmDbEngine +")", notes: notes});
    }
  });
});
/*
當(dāng)用戶點(diǎn)擊ADD按鈕時app.get('/add', ...)內(nèi)的函數(shù)就會被調(diào)用,瀏覽器會發(fā)出一個發(fā)往/add的HTTP GET請求。
這個函數(shù)使用addedit.html模板來創(chuàng)建一個表單,讓用于通過這個表單輸入標(biāo)簽,然后通過單擊SUBMIT按鈕提交,
當(dāng)用戶提交表單,瀏覽器就會發(fā)出一個HTTP POST請求,app.post('/add', ...)內(nèi)的函數(shù)就會被調(diào)用,
用戶輸入的數(shù)據(jù)會被存放在請求主體中,而請求主體會被bodyParser(app.use(express.bodyParser()))中間件處理并存放在req.body中
 */
app.get('/add', function(req, res){
  res.render('addedit.html', {title: "Notes ("+ nmDbEngine +")", postpath: '/add', note: notesdb.emptyNote});
});
app.post('/add', function(req, res){
  notesdb.add(req.body.author, req.body.note,
    function(error){
      if (error) throw error;
      res.redirect('/view');
    });
});
app.get('/del', parseUrlParams, function(req, res){
  notesdb.delete(req.urlP.query.id,
    function(error){
      if (error) throw error;
      res.redirect('/view');
    });
});
app.get('/edit', parseUrlParams, function(req, res){
  notesdb.findNoteById(req.urlP.query.id,
    function(error, note){
      if (error) throw error;
      res.render('addedit.html',
        {title: "Notes ("+ nmDbEngine +")", postpath: '/edit', note: note});
    });
});
app.post('/edit', function(req, res){
  notesdb.edit(req.body.id, req.body.author, req.body.note,
    function(error){
      if (error) throw error;
      res.redirect('/view');
    });
});
app.get('/login', function(req, res){
  res.render('login.html', {title: "Notes LOGIN ("+ nmDbEngine +")"});
});
app.post('/login', function(req, res){
  //此處可以添加檢查用戶信息的邏輯
  //...
  res.cookie('notesaccess', 'AOK');
  res.redirect('/view');
});
app.listen(3000);

show.js

//控制臺顯示
var util = require('util');
var notesdb = require('./notesdb-sqlite3');
// var notesdb = require('./notesdb-mongoose');
notesdb.connect(function(error){
  if (error) throw error;
});
notesdb.forAll(function(error, row){
  util.log('ROW: ' + util.inspect(row));
}, function(error){
  if (error) throw error;
  util.log('ALL DONE');
  notesdb.disconnect(function(err){});
});

前臺頁面在views-sqlite3目錄下

layout.html

<!DOCTYPE html>
<html>
<head>
  <title><%= title%></title>
</head>
<body>
  <h1><%= title%></h1>
  <p><a href='/view'>View</a> | <a href='/add'>Add</a></p>
</body>
</html>

viewnotes.html

<% include layout.html %>
<table><% notes.forEach(function(note){ %>
  <tr>
    <td>
      <p><%=new Date(note.ts).toString()%>: by <b><%=note.author%></b></p>
      <p><%=note.note%></p>
    </td>
    <td>
      <form method="get" action="/del">
        <input type="submit" value="Delete" />
        <input type="hidden" name="id" value="<%=note.ts%>" />
      </form>
      <br/>
      <form method="get" action="/edit">
        <input type="submit" value="Edit" />
        <input type="hidden" name="id" value="<%=note.ts%>" />
      </form>
    </td>
  </tr>
  <% }); %>
</table>

addedit.html

<% include layout.html %>
<form method="post" action="<%=postpath%>">
  <% if (note){ %>
  <input type="hidden" name="id" value="<%=note.ts%>" />
  <% } %>
  <input type="text" name="author" value="<%=note.author%>" />
  <br/>
  <textarea rows="5" cols="40" name="note">
    <%=note.note%>
  </textarea>
  <br/>
  <input type="submit" value="Submit" />
</form>

login.html

<% include layout.html %>
<form method="POST" action="/login">
  <p>Click the <i>Login</i> to log in.</p>
  <input type="submit" value="Login" />
</form>

node setup.js

node app.js

希望本文所述對大家nodejs程序設(shè)計有所幫助。

相關(guān)文章

  • 使用Koa實(shí)現(xiàn)一個獲取視頻播放地址的接口

    使用Koa實(shí)現(xiàn)一個獲取視頻播放地址的接口

    在本節(jié)課中,我們將學(xué)習(xí)如何使用 Koa 實(shí)現(xiàn)一個獲取視頻播放地址的接口,我們將創(chuàng)建一個控制器,通過視頻 ID 獲取播放地址,并設(shè)置相應(yīng)的路由,最后,我們將使用 Postman 進(jìn)行測試,感興趣的朋友可以參考下
    2024-05-05
  • Node.js中的流(Stream)介紹

    Node.js中的流(Stream)介紹

    這篇文章主要介紹了Node.js中的流(Stream)介紹,本文講解了什么是流、pipe方法、流的分類、Readable流狀態(tài)的切換等內(nèi)容,需要的朋友可以參考下
    2015-03-03
  • Node.js事件循環(huán)(Event Loop)和線程池詳解

    Node.js事件循環(huán)(Event Loop)和線程池詳解

    這篇文章主要介紹了Node.js事件循環(huán)(Event Loop)和線程池詳解,這篇文章比較淺顯地探討了有關(guān)事件循環(huán)的內(nèi)部運(yùn)作機(jī)制和技術(shù)細(xì)節(jié),都是經(jīng)過深思熟慮的,需要的朋友可以參考下
    2015-01-01
  • 如何配置nodejs的環(huán)境變量

    如何配置nodejs的環(huán)境變量

    這篇文章主要介紹了如何配置nodejs的環(huán)境變量問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • node使用Mongoose類庫實(shí)現(xiàn)簡單的增刪改查

    node使用Mongoose類庫實(shí)現(xiàn)簡單的增刪改查

    Mongoose是在nodejs環(huán)境中對MongoDB數(shù)據(jù)庫操作的封裝,這篇文章主要介紹了node使用Mongoose類庫實(shí)現(xiàn)簡單的增刪改查,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11
  • 一文教會你從Windows中完全刪除node.js

    一文教會你從Windows中完全刪除node.js

    作為新手nodejs卸載后安裝就總出錯,下面這篇文章主要給大家介紹了關(guān)于如何從Windows中完全刪除node.js的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-08-08
  • 一會帶你學(xué)會用Webpack搭建開發(fā)環(huán)境并打包代碼

    一會帶你學(xué)會用Webpack搭建開發(fā)環(huán)境并打包代碼

    這篇文章主要給大家介紹了關(guān)于如何用Webpack搭建開發(fā)環(huán)境并打包的相關(guān)資料,webpack是一個現(xiàn)代JavaScript應(yīng)用程序的靜態(tài)模塊打包器(module bundler),需要的朋友可以參考下
    2023-08-08
  • 創(chuàng)建簡單的node服務(wù)器實(shí)例(分享)

    創(chuàng)建簡單的node服務(wù)器實(shí)例(分享)

    下面小編就為大家?guī)硪黄獎?chuàng)建簡單的node服務(wù)器實(shí)例(分享)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • Node.js進(jìn)程退出的深入理解

    Node.js進(jìn)程退出的深入理解

    NodeJS可以感知和控制自身進(jìn)程的運(yùn)行環(huán)境和狀態(tài),也可以創(chuàng)建子進(jìn)程并與其協(xié)同工作,這使得NodeJS可以把多個程序組合在一起共同完成某項(xiàng)工作,下面這篇文章主要給大家介紹了關(guān)于Node.js進(jìn)程退出的相關(guān)資料,需要的朋友可以參考下
    2022-04-04
  • Node.js fs模塊(文件模塊)創(chuàng)建、刪除目錄(文件)讀取寫入文件流的方法

    Node.js fs模塊(文件模塊)創(chuàng)建、刪除目錄(文件)讀取寫入文件流的方法

    這篇文章主要介紹了Node.js fs模塊(文件模塊)創(chuàng)建、刪除目錄(文件)讀取寫入文件流的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-09-09

最新評論