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

node.js +mongdb實現(xiàn)登錄功能

 更新時間:2020年06月18日 10:59:15   作者:ITHERS  
這篇文章主要介紹了node.js +mongdb實現(xiàn)登錄功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

一、開發(fā)環(huán)境準備

npm init -y //初始化倉庫
npm i koa koa-router -S //安裝koa和路由模塊
npm i nodemon -S

二、開啟node服務

//index.js
const koa = require("koa");
const router = require("koa-router")();
const app = new koa();
router.get("/",async ctx=>{
 ctx.body = "我是首頁"
})
app.use(router.routes());
app.listen(8080);

三、配置art-template

3-1 安裝依賴

npm install koa-art-template art-template -S

四、拆分路由系統(tǒng)

在這里插入圖片描述

//routers-index.js
const router = require("koa-router")();
router.get("/", async ctx => {
 await ctx.render('index');
})
module.exports = router;
//index.js
const koa = require("koa");
const router = require('./routers/index.js');
const render = require('koa-art-template');
const app = new koa();
const path = require("path");
render(app, {
 root: path.join(__dirname, 'views'),
 extname: '.html',
 debug: process.env.NODE_ENV !== 'production'
});

app.use(router.routes());
app.listen(8080);

五、初始化項目的配置文件

//init-config.js
const path = require("path");
const render = require('koa-art-template');
const router = require('../routers/index');
function initConfig(app) {
 // process.cwd()可以讀取項目的路徑
 render(app, {
  root: path.join(process.cwd(), 'views'),
  extname: '.html',
  debug: process.env.NODE_ENV !== 'production'
 });

 app.use(router.routes());
}
module.exports = initConfig;
//index.js
const koa = require("koa");
const app = new koa();
let initConfig = require('./init/config');
initConfig(app);
app.listen(8080);

六、MVC設計項目結構

model -->數(shù)據(jù)層
views -->視圖層
controllers -->視圖控制層

在這里插入圖片描述

//routers--index.js
const router = require("koa-router")();
const controllers = require("../controllers");
// 只寫文件名默認會讀取文件里的index.js
router.get("/", controllers.index)
module.exports = router;
//controller--index.js
let index = async ctx=>{
 await ctx.render('index');
}
module.exports = {
 index
}

七、獲取post傳值

//1.安裝依賴
npm i koa-bodyparser -S
//2.進行項目的配置
var Koa = require('koa');
var bodyParser = require('koa-bodyparser');

var app = new Koa();
app.use(bodyParser());

app.use(async ctx => {
 //3.取值 ctx.request.body
 ctx.body = ctx.request.body;
});

八、連接數(shù)據(jù)庫

npm i mongoose -S
//models --db.js
const mongoose = require('mongoose');
mongoose.connect( 'mongodb://127.0.0.1:27017/studentManage', {useNewUrlParser: true},(err)=>{
 if(err) throw err;
 console.log("database連接成功")
});
module.exports = mongoose;
//models --user.js
var mongoose = require('./db');
var UserSchema = new mongoose.Schema({
 name:String,
 pwd:String
})
var User = mongoose.model('User',UserSchema,'user');
module.exports = User;
//controllers -- index.js
let doLogin = async ctx=>{
 let {user,pwd} = ctx.request.body;
 let data = await UserModel.find({name:user,pwd});
 console.log(data);
 // 數(shù)據(jù)庫中有對應的用戶名密碼則數(shù)組有東西,沒有則數(shù)組沒東西
 if(data.length>0){
  /* 登錄成功 */
  ctx.body = "登錄成功"
 }else{
  ctx.body = "用戶名和密碼不存在"
 } 
}

總結

到此這篇關于node.js +mongdb實現(xiàn)登錄功能的文章就介紹到這了,更多相關node.js mongdb實現(xiàn)登錄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Windows系統(tǒng)下使用Sublime搭建nodejs環(huán)境

    Windows系統(tǒng)下使用Sublime搭建nodejs環(huán)境

    最近在研究Nodejs開發(fā),俗話說,工欲善其事,必先利其器,當然要找到一款用著順手的編輯器作為開始。這里我們選擇的是Sublime Text 3,除了漂亮的用戶界面,最吸引我的就是它的插件擴展功能以及跨平臺特性。
    2015-04-04
  • Nodejs訪問網(wǎng)絡并解析返回的json的實現(xiàn)方法

    Nodejs訪問網(wǎng)絡并解析返回的json的實現(xiàn)方法

    本文主要介紹了Nodejs訪問網(wǎng)絡并解析返回的json的實現(xiàn)方法,文中根據(jù)實例編碼詳細介紹的十分詳盡,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Node.js創(chuàng)建一個Express服務的方法詳解

    Node.js創(chuàng)建一個Express服務的方法詳解

    這篇文章主要介紹了Node.js創(chuàng)建一個Express服務的方法,結合實例形式分析了node.js創(chuàng)建Express服務的具體步驟、實現(xiàn)方法及相關操作技巧,需要的朋友可以參考下
    2020-01-01
  • Node.js之http模塊的用法

    Node.js之http模塊的用法

    這篇文章主要介紹了Node.js之http模塊的用法,對Node.js感興趣的同學,可以參考下
    2021-04-04
  • nodejs個人博客開發(fā)第六步 數(shù)據(jù)分頁

    nodejs個人博客開發(fā)第六步 數(shù)據(jù)分頁

    這篇文章主要為大家詳細介紹了nodejs個人博客開發(fā)的數(shù)據(jù)分頁,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • nodejs如何獲取當前連接的網(wǎng)絡ip

    nodejs如何獲取當前連接的網(wǎng)絡ip

    這篇文章主要介紹了nodejs如何獲取當前連接的網(wǎng)絡ip問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • Node中完整的?node?addon?實現(xiàn)流程

    Node中完整的?node?addon?實現(xiàn)流程

    這篇文章主要介紹了Node中完整的node?addon實現(xiàn)流程,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-09-09
  • node+vue實現(xiàn)文件上傳功能

    node+vue實現(xiàn)文件上傳功能

    這篇文章主要介紹了node+vue實現(xiàn)文件上傳功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • Nodejs抓取html頁面內(nèi)容(推薦)

    Nodejs抓取html頁面內(nèi)容(推薦)

    這篇文章主要介紹了Nodejs抓取html頁面內(nèi)容的關鍵代碼,另外還給大家附上了nodejs抓取網(wǎng)頁內(nèi)容,非常不錯,對node.js抓取頁面內(nèi)容感興趣的朋友一起學習吧
    2016-08-08
  • 淺談手寫node可讀流之流動模式

    淺談手寫node可讀流之流動模式

    這篇文章主要介紹了淺談手寫node可讀流之流動模式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-06-06

最新評論