亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

NodeJS學(xué)習(xí)筆記之Connect中間件應(yīng)用實例

 更新時間:2015年01月27日 16:03:35   投稿:hebedich  
前面我們介紹了幾篇內(nèi)容的connect中間件的基礎(chǔ)知識,今天我們來實例應(yīng)用一下,做個記事本的小應(yīng)用,希望大家能夠喜歡。

一,開篇分析

大家好哦,大熊君又來了,昨天因為有點個人的事沒有寫博客,今天又出來了一篇,這篇主要是寫一個記事本的小應(yīng)用,前面的文章,

我也介紹過“Connect”中間件的使用以及“Mongodb”的用法,今天就結(jié)合這兩個中間件,寫個實際的例子,不斷完善和重構(gòu),已達(dá)到

充分學(xué)習(xí)的目的。好了,廢話不說了,直接進入主題。

二,需求分析

(1),用戶注冊,登錄功能(沒有涉及很復(fù)雜的交互場景,注冊時會有用戶判斷是否已存在)。

(2),用戶登錄成功,進入筆記管理系統(tǒng)的后臺(筆記模塊的增刪改查功能)。

(3),用戶可以具有簡單的權(quán)限劃分(管理員,注冊用戶)。

(4),界面比較簡單,以學(xué)習(xí)為主。

三,開始設(shè)計應(yīng)用(第一部分)

(1),建立用戶登錄頁面,代碼如下:

復(fù)制代碼 代碼如下:

<!doctype html>
<html>
    <head>
        <title>Bigbear記事本應(yīng)用登錄</title>
        <meta content="IE=8" http-equiv="X-UA-Compatible"/>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <style type="text/css">
            .note-title {
                margin-bottom : 45px ;
                background : #6699cc ;
                font-size : 14px ;
                font-weight : bold ;
                color : #fff;
                font-family:arial ;
                height : 24px ;
                line-height : 24px ;
            }
            a {
                color : #336699;
                font-family:arial ;
                font-size : 14px ;
                font-weight : bold ;
            }
        </style>
        <script src="js/index.js"></script>
    </head>
    <body>
        <div class="note-title">Bigbear記事本應(yīng)用登錄</div>
            <form action="/login" method="post">
                <span>用戶名:</span><input type="text" name="name" /><br/><br/>
                <span>密&nbsp;&nbsp;碼:</span><input type="password" name="password" />
                <input type="submit" value="登錄" />
                <a href="reg.html">我要注冊</a>
            </form>
    </body>
</html>

  效果圖:

(2),建立用戶注冊頁面,代碼如下:

復(fù)制代碼 代碼如下:

 <!doctype html>
 <html>
     <head>
         <title>Bigbear記事本應(yīng)用注冊</title>
         <meta content="IE=8" http-equiv="X-UA-Compatible"/>
         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
         <style type="text/css">
             .note-title {
                 margin-bottom : 45px ;
                 background : #ff3300 ;
                 font-size : 14px ;
                 font-weight : bold ;
                 color : #fff;
                 font-family:arial ;
                 height : 24px ;
                 line-height : 24px ;
             }
         </style>
         <script src="js/index.js"></script>
     </head>
     <body>
         <div class="note-title">Bigbear記事本應(yīng)用注冊</div>
             <form action="/reg" method="post">
                 <span>用戶名:</span><input type="text" name="name" /><br/><br/>
                 <span>密&nbsp;&nbsp;碼:</span><input type="password" name="password" /><br/><br/>
                 <input type="submit" value="注冊" />
             </form>
     </body>
 </html>

  效果圖:

(3),建立“Mongodb”連接代碼,如下:

復(fù)制代碼 代碼如下:

 var mongodb = require("mongodb") ;
 var server = new mongodb.Server("localhost",27017,{
     auto_reconnect : true
 }) ;
 var conn = new mongodb.Db("bb",server,{
     safe : true
 }) ;
 conn.open(function(error,db){
     if(error) throw error ;
     console.info("mongodb connected !") ;
 }) ;
 exports = module.exports = conn ;

(4),建立模型實體類“User”,如下:

復(fù)制代碼 代碼如下:

 var conn = require("../conn") ;
 function User(user){
     this.name = user["name"] ;
     this.password = user["password"] ;
 } ;
 User.prototype.save = function(callback){
     var that = this ;
     conn.collection("users",{
         safe : true
     },function(error,collection){
         if(error) return conn.close() ;
         collection.findOne({   // 判斷此用戶是否存在
             name : that.name
         },function(error,user){
             if(error) return conn.close() ;
             if(!user){
                 collection.insert({
                     name : that.name + "" ,
                     password : that.password + ""
                 },{
                     safe : true
                 },function(error,user){
                     if(error) return conn.close() ;
                     callback && callback(user) ;
                     conn.close() ;
                 }) ;       
             }
             else{
                 callback("User has registed !") ;
             }
         }) ;
     }) ;
 } ;
 User.login = function(name,password,callback){
     conn.collection("users",{
         safe : true
     },function(error,collection){
         if(error) return conn.close() ;
         collection.findOne({
             name : name ,
             password : password
         },function(error,user){
             if(error) return conn.close() ;
             callback && callback(user) ;
             conn.close() ;
         }) ;
     }) ;
 } ;
 exports = module.exports = User ;

  效果圖:

(5),建立應(yīng)用程序“app”,如下:

復(fù)制代碼 代碼如下:

 // app.js
 var connect = require("./lib/connect") ;
 var user = require("./models/user") ;
 var app = connect.createServer() ;
 app .use(connect.logger("dev"))
 .use(connect.query())
 .use(connect.bodyParser())
 .use(connect.cookieParser())
 .use(connect.static(__dirname + "/views"))
 .use(connect.static(__dirname + "/public"))
 .use("/login",function(request,response,next){
     var name = request.body["name"] ;
     var password = request.body["password"] ;
     user.login(name,password,function(user){
         if(user){
             response.end("Welcome to:" + user["name"] + " ^_^ ... ...") ;   
         }
         else{
             response.end("User:" + name + " Not Register !")    ;
         }
     }) ;
 })
 .use("/reg",function(request,response,next){
     var name = request.body["name"] ;
     var password = request.body["password"] ;
     new user({
         name : name ,
         password : password
     }).save(function(user){
         if(user && user["name"]){
           response.end("User:" + name + "Register Done !")    ;   
         }
         else{
           response.end("User: " + name + "has registed !") ; 
         }
     }) ;
 })
 .listen(8888,function(){
     console.log("Web Server Running On Port ---> 8888 .") ;
 }) ;

  說明一下:

   ?。?)“connect.query()”------處理“Get”請求參數(shù)解析。

   ?。?)“connect.bodyParser()”------處理“Post”請求參數(shù)解析。

   ?。?)“connect.static(__dirname + "/views"),connect.static(__dirname + "/public")”

     分別代表模板視圖“html”以及靜態(tài)資源如“js,css,jpg,gif”的資源目錄。

     以下是這個應(yīng)用的目錄結(jié)構(gòu):

四,總結(jié)一下

 ?。?),掌握NodeJs操作數(shù)據(jù)庫的基本操作語句。

  (2),劃分層級,如模型,視圖,路由。

 ?。?),不斷優(yōu)化和修改本文的例子(比如注冊驗證用戶是否存在,可以獨立出“UserManager”做一層代理完成用戶驗證和保存的動作)。

 ?。?),明天繼續(xù)完成后續(xù)的功能,盡請期待。

相關(guān)文章

  • 淺談node.js中async異步編程

    淺談node.js中async異步編程

    本文嘗試結(jié)合Marc Fasel的指導(dǎo)思想和筆者的實踐經(jīng)驗來介紹一些NodeJS的異步編程風(fēng)格,希望對NodeJS的初學(xué)者有所啟發(fā)。
    2015-10-10
  • Node 使用express-http-proxy 做api網(wǎng)關(guān)的實現(xiàn)

    Node 使用express-http-proxy 做api網(wǎng)關(guān)的實現(xiàn)

    這篇文章主要介紹了Node 使用express-http-proxy 做api網(wǎng)關(guān)的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • Node.js卸載與重裝及zip與msi安裝詳解

    Node.js卸載與重裝及zip與msi安裝詳解

    Node.js是一個JavaScript運行環(huán)境,可以使JavaScript這類腳本語言編寫出來的代碼運行速度獲得極大提升,下面這篇文章主要給大家介紹了關(guān)于Node.js卸載與重裝及zip與msi安裝的相關(guān)資料,需要的朋友可以參考下
    2023-01-01
  • Node.js原理阻塞和EventEmitter及其繼承的運用實戰(zhàn)

    Node.js原理阻塞和EventEmitter及其繼承的運用實戰(zhàn)

    這篇文章主要介紹了Node.js原理阻塞和EventEmitter及其繼承的運用實戰(zhàn),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-08-08
  • nodejs創(chuàng)建簡易web服務(wù)器與文件讀寫的實例

    nodejs創(chuàng)建簡易web服務(wù)器與文件讀寫的實例

    下面小編就為大家?guī)硪黄猲ode js系列課程-創(chuàng)建簡易web服務(wù)器與文件讀寫的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • nodejs log4js 使用詳解

    nodejs log4js 使用詳解

    這篇文章主要介紹了nodejs log4js 使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • 詳解node登錄接口之密碼錯誤限制次數(shù)(含代碼)

    詳解node登錄接口之密碼錯誤限制次數(shù)(含代碼)

    這篇文章主要介紹了nodejs登錄接口之密碼錯誤限制次數(shù)(含代碼),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • npm報錯"A?complete?log?of?this?run?can?be?found?in:"的解決辦法

    npm報錯"A?complete?log?of?this?run?can?be?found?

    這篇文章主要給大家介紹了關(guān)于npm報錯"A?complete?log?of?this?run?can?be?found?in:"的解決辦法,文中通過實例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2023-04-04
  • node.js降低版本的方式詳解(解決sass和node.js沖突問題)

    node.js降低版本的方式詳解(解決sass和node.js沖突問題)

    這篇文章主要介紹了node.js降低版本的方式(解決sass和node.js沖突),本文是因為sass版本和node版本不匹配(可以找一下對應(yīng)的版本),本文給大家詳細(xì)講解,需要的朋友可以參考下
    2023-02-02
  • node實現(xiàn)登錄圖片驗證碼的示例代碼

    node實現(xiàn)登錄圖片驗證碼的示例代碼

    這篇文章主要介紹了node實現(xiàn)登錄圖片驗證碼的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04

最新評論