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

nodejs+koa2 實(shí)現(xiàn)模仿springMVC框架

 更新時(shí)間:2020年10月21日 14:20:38   作者:24ks  
這篇文章主要介紹了nodejs+koa2 實(shí)現(xiàn)模仿springMVC框架,本文通過(guò)實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

koa2-MVC架構(gòu)

---------后端技術(shù)做前端

環(huán)境:nodejs

開發(fā)工具:Visual Studio Code(下文簡(jiǎn)稱:VSC)

環(huán)境安裝,工具安裝及中文自行百度,環(huán)境調(diào)整好后開始進(jìn)入正題。

1、在硬盤上新增一個(gè)文件夾,打開VSC,點(diǎn)擊‘添加工作區(qū)文件夾',如果沒有歡迎‘使用頁(yè)面',點(diǎn)擊--文件--新建窗口,效果如下圖:

2、添加vsc調(diào)試。Shift+ctrl+p,輸入框內(nèi)輸入:launch.json

選擇剛剛的文件夾

3、目錄結(jié)構(gòu)

從低到高one by one

3-1、package.json

{
 "name": "koa2mcv",
 "version": "1.0.0",
 "description": "Hello Koa 2 example with MVC",
 "main": "app.js",
 "scripts": {
  "start": "node app.js"
 },
 "author": "baba",
 "dependencies": {
  "koa": "2.11.0",
  "koa-router": "8.0.8",
  "koa-bodyparser": "4.3.0",
  "koa-static-plus": "0.1.1",
  "koa-view": "2.1.3",
  "koa-jwt": "4.0.0",
  "koa-log4": "2.3.2",
  "jsonwebtoken": "8.5.1",
  "nunjucks": "3.2.1",
  "mime": "2.4.5",
  "mz": "2.7.0"
 }
}

參數(shù)介紹:name項(xiàng)目名稱、version版本號(hào)、description項(xiàng)目描述、main項(xiàng)目啟動(dòng)文件、scripts啟動(dòng)快捷設(shè)置,author作者,dependencies第3方中間件名稱及版本。

3-2、app.js

//啟動(dòng)服務(wù)
require('./config/init').startServer();

啟動(dòng)相關(guān)配置,封裝到config/init.js中,啟動(dòng)文件直接引用即可。

3-3、views存放html頁(yè)面

3-4、static存放靜態(tài)文件,css,js,font等

3-5、src存放業(yè)務(wù)控制,類似于springMVC中的controller、service。

3-6、config存放核心配置文件。

3-6-1、init.js項(xiàng)目核心。

異常友好處理

function handler(){
 return async (ctx, next) => {

  const start = new Date().getTime();

  var urlReq=ctx.request.url;
  if(urlReq !== '/favicon.ico'){

   console.log(`請(qǐng)求地址:${ctx.request.method} ${urlReq}`);
   try {

    let params =Object.assign({}, ctx.request.query, ctx.request.body);

    if(config["token"].excludeUrl.indexOf(urlReq) == -1 && !tokenFunction.varifyToken(params.token)){
     ctx.status =401;
    }else{
     await next();
    }
   } catch (error) {
    ctx.status=401;
    console.log(`錯(cuò)誤!無(wú)法獲取token參數(shù)`);
   }finally{

     let err={};
     if(!ctx.status){
      err.status = 500;
     }else if(ctx.status==200){
      return;
     }else{
      err.status = ctx.status;
     }

     switch(err.status){
      case 404:
       err.url = config["server-name"]+'/static/public/404.html';
       err.message="資源不存在!";
       break;
      case 401:
       err.url = config["server-name"]+'/static/public/10000.html';
       err.message="登陸失效!請(qǐng)重新登陸!";
       break;
      case 500:
       err.url = config["server-name"]+'/static/public/500.html';
       err.message="系統(tǒng)內(nèi)部錯(cuò)誤!";
       break;
     }

     switch(ctx.request.type){
      case 'application/json':
       ctx.type = 'application/json';
       ctx.body = {errorCode:err.errorCode,message: err.message}
       break;
      default:

       ctx.type = 'text/html';
       ctx.redirect(err.url);
       break;
     }
   }
  }
  const ms = new Date().getTime() - start;
  console.log(`請(qǐng)求消耗時(shí)間: ${ms}ms`);
 }
}

路由配置

function controller(){

 const router = new koaRouter({
  prefix: config["server-name"]
 });

 function findJsonFile(rootpathStr){

  fs.readdirSync(rootpathStr).forEach(function (item, index) {

   let fPath = path.join(rootpathStr,item);

   let stat = fs.statSync(fPath);

   if(stat.isDirectory() === true) {
    findJsonFile(fPath);
   }

   if (stat.isFile() === true&&fPath.endsWith('.js')) {

    var mapping = require(fPath);
    for (var url in mapping) {
     if (url.startsWith('GET ')) {
      router.get(url.substring(4), mapping[url]);
     } else if (url.startsWith('POST ')) {
      router.post(url.substring(5), mapping[url]);
     } else if (url.startsWith('PUT ')) {
      router.put(url.substring(4), mapping[url]);
     } else if (url.startsWith('DELETE ')) {
      router.del(url.substring(7), mapping[url]);
     }
     console.log(`注冊(cè) URL: ${url}`);
    }
   }
  });
 }

 findJsonFile(rootpath + 'src');
 return router.routes();
}

視圖渲染

function templating() {
 var
  autoescape = config['templating-autoescape'] === null ? true : config['templating-autoescape'],
  noCache = config['templating-noCache'] === null ? false : config['templating-noCache'],
  watch = config['templating-watch'] === null ? false : config['templating-watch'],
  throwOnUndefined = config['templating-throwOnUndefined'] === null ? false :config['templating-throwOnUndefined'],
  env = new nunjucks.Environment(
   new nunjucks.FileSystemLoader(rootpath+'views', {
    noCache: noCache,
    watch: watch,
   }), {
    autoescape: autoescape,
    throwOnUndefined: throwOnUndefined
   });
 if (config['templating-filters'] != null) {
  for (var f in config['templating-filters']) {
   env.addFilter(f, config['templating-filters'][f]);
  }
 }
 return async (ctx, next) => {
  ctx.render = function (view, model) {
   ctx.response.body = env.render(view, Object.assign({}, ctx.state || {}, model || {}));
   ctx.response.type = 'text/html';
  };
  await next();
 };
}

啟動(dòng)構(gòu)建

function startServer(){

 const app = new koa();

 app.use(koaStaticPlus(rootpath+'static', {
  pathPrefix: config["server-name"]+'/static'
 })
 );
 
 app.use(koaBodyParser());
 
 app.use(handler());
 
 app.use(templating());
 
 app.use(controller());
 
 app.listen(config["server-port"]);

}

3-6-2、config.js項(xiàng)目參數(shù)配置。

module.exports ={
 'server-name':'/koa',
 'server-port':3000,
 "templating-noCache":true,
 "templating-watch":true,
 "templating-autoescape":null,
 "templating-throwOnUndefined":null,
 "templating-filters":null,
 "token":{
  "excludeUrl":[
  "/koa/login",
  "/koa/dologin"
  ],
  "timeout":1000 * 60 * 60 * 24 * 7,
  "secret":"jiaobaba"
 }
 

}

3-6-3、token.js項(xiàng)目token相關(guān)方法封裝。

const jwt = require("jsonwebtoken");
const config = require('./config');
/**
 * 創(chuàng)建token的方法
 */
let createToken = (data)=>{
 let obj = {};
 //存入token的數(shù)據(jù)
 obj.data = data || {};
 //token的創(chuàng)建時(shí)間
 obj.ctime = (new Date()).getTime();
 return jwt.sign(obj,config["token"].secret);
}
/**
 * 驗(yàn)證token是否合法的方法
 * @param {*} token 
 */
let varifyToken = (token)=>{
 let result = null;
 try{
  let {data,ctime,expiresIn} = jwt.verify(token,config["token"].secret);
  let nowTime = (new Date()).getTime();
  if(nowTime-ctime<config["token"].timeout){
   result = data;  
  }
 }catch(error){

 }
 return result;
}

module.exports = {
 createToken,
 varifyToken
};

3-6-4、logger.js項(xiàng)目日志配置文件。

4、項(xiàng)目結(jié)構(gòu)構(gòu)建結(jié)束,接著引入所有依賴包,在終端中執(zhí)行‘npm install' ,會(huì)下載package.json中dependencies所有包,以及這些包所依賴的包。

執(zhí)行后項(xiàng)目結(jié)構(gòu)會(huì)增加兩個(gè)文件

5、編寫測(cè)試用例,在src下新建hello.js。

//token
const token = require('../config/token');

var fn_hello = async (ctx, next) => {
 var name = ctx.params.name;
 ctx.response.body = `<h1>Hello, ${name}!</h1>`;
};


var fn_index = async (ctx, next) => {
 ctx.response.body = `<h1>Index</h1>
  <form action="/koa/signin" method="post">
   <p>Name: <input name="name" value=""></p>
   <p>Password: <input name="password" type="password"></p>
   <p><input type="submit" value="Submit"></p>
  </form>`;
};

var fn_signin = async (ctx, next) => {
 var
  name = ctx.request.body.name || '',
  password = ctx.request.body.password || '';
 console.log(`登陸名: ${name}, 密碼: ${password}`);
  ctx.response.body = `<h1>Welcome, ${name}!</h1><br/>you token:<br/>${token.createToken({user: name,password: password})}`;
 
};

module.exports = {
 'GET /hello/:name': fn_hello,
 'GET /login': fn_index,
 'POST /dologin': fn_signin
};

6、啟動(dòng)項(xiàng)目

啟動(dòng)成功

測(cè)試訪問(wèn):http://127.0.0.1:3000/koa/login

輸入值獲取token

先不用帶token進(jìn)行訪問(wèn)

攔截成功

帶上token 進(jìn)行訪問(wèn)

測(cè)試成功!

到此這篇關(guān)于nodejs+koa2 實(shí)現(xiàn)模仿springMVC的文章就介紹到這了,更多相關(guān)nodejs+koa2 springMVC內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • nodejs中轉(zhuǎn)換URL字符串與查詢字符串詳解

    nodejs中轉(zhuǎn)換URL字符串與查詢字符串詳解

    這篇文章主要介紹了nodejs中轉(zhuǎn)換URL字符串與查詢字符串詳解,需要的朋友可以參考下
    2014-11-11
  • Node.js中Sequelize?hook的使用方法小結(jié)

    Node.js中Sequelize?hook的使用方法小結(jié)

    Sequelize?提供了多個(gè)?hook,用于在執(zhí)行數(shù)據(jù)庫(kù)操作時(shí)執(zhí)行一些自定義邏輯,本文為大家整理了一些常用的?Sequelize?hook?列表及其作用,希望對(duì)大家有所幫助
    2024-02-02
  • node+vue實(shí)現(xiàn)用戶注冊(cè)和頭像上傳的實(shí)例代碼

    node+vue實(shí)現(xiàn)用戶注冊(cè)和頭像上傳的實(shí)例代碼

    本篇文章主要介紹了node+vue實(shí)現(xiàn)用戶注冊(cè)和頭像上傳的實(shí)例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下
    2017-07-07
  • Sequelize中用group by進(jìn)行分組聚合查詢

    Sequelize中用group by進(jìn)行分組聚合查詢

    大家都知道在SQL查詢中,分組查詢是較常用的一種查詢方式。分組查詢是指通過(guò)GROUP BY關(guān)鍵字,將查詢結(jié)果按照一個(gè)或多個(gè)字段進(jìn)行分組,分組時(shí)字段值相同的會(huì)被分為一組。在Node.js基于Sequelize的ORM框架中,同樣支持分組查詢,使用非常簡(jiǎn)單方便。下面來(lái)看看詳細(xì)的介紹。
    2016-12-12
  • 刪除node_modules文件夾太慢的解決方案

    刪除node_modules文件夾太慢的解決方案

    這篇文章主要介紹了刪除node_modules文件夾太慢的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • windows系統(tǒng)上完全卸載并重裝Node的步驟(親測(cè)可用)

    windows系統(tǒng)上完全卸載并重裝Node的步驟(親測(cè)可用)

    對(duì)于Windows平臺(tái)來(lái)說(shuō),所有的應(yīng)用程序,其安裝卸載都是一樣的,node.js也不例外,但是還是很多用戶不明白,下面這篇文章主要給大家介紹了關(guān)于windows系統(tǒng)上完全卸載并重裝Node的步驟,需要的朋友可以參考下
    2023-03-03
  • 淺談Express異步進(jìn)化史

    淺談Express異步進(jìn)化史

    本篇文章主要介紹了淺談Express異步進(jìn)化史 ,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-09-09
  • Node.js中ES6模塊化及Promise對(duì)象

    Node.js中ES6模塊化及Promise對(duì)象

    這篇文章主要介紹了Node.js中ES6模塊化及Promise對(duì)象,node.js?遵循了?CommonJS?的模塊化規(guī)范,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-07-07
  • 搭建一個(gè)nodejs腳手架的方法步驟

    搭建一個(gè)nodejs腳手架的方法步驟

    這篇文章主要介紹了如何搭建一個(gè)nodejs腳手架的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • Node.js Event Loop各階段講解

    Node.js Event Loop各階段講解

    今天小編就為大家分享一篇關(guān)于Node.js Event Loop各階段講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-03-03

最新評(píng)論