<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          iijs簡(jiǎn)單輕量級(jí) MVC 框架

          聯(lián)合創(chuàng)作 · 2023-09-19 11:19

          iijs是一個(gè)基于nodejs+koa2構(gòu)建的簡(jiǎn)單輕量級(jí)MVC框架,最低依賴僅僅koa和koa-router。

          特點(diǎn)

          本MVC框架極為輕量小巧,又自由靈活,使用簡(jiǎn)單,功能又足夠強(qiáng)大,可開發(fā)簡(jiǎn)單的頁(yè)面展示網(wǎng)站,可以開發(fā)pai接口應(yīng)用,也可支撐復(fù)雜的多應(yīng)用網(wǎng)站。

          使用

          1. 安裝 npm i iijs

          應(yīng)用結(jié)構(gòu)

          ├── app             //應(yīng)用目錄 (非必需,可更改)
          │  ├── Controller   //控制器目錄 (非必需,可更改)
          │  │  └── index.js  //控制器
          │  ├── view         //模板目錄 (非必需,可更改)
          │  │  └── index     //index控制器模板目錄 (非必需,可更改)
          │  │     └── index.htm //模板
          │  ├── model        //模型目錄 (非必需,可更改)
          │  ├── logic        //邏輯目錄 (非必需,可更改)
          │  └── ****         //其他目錄 (非必需,可更改)
          ├── app2            //應(yīng)用2目錄 (非必需,可更改)
          ├── common          //公共應(yīng)用目錄 (非必需,可更改)
          ├── config          //配置目錄 (非必需,不可更改)
          │  ├── app.js       //APP配置 (非必需,不可更改)
          │  ├── route.js     //路由配置 (非必需,不可更改)
          │  └── ****         //其他配置 (非必需,可更改)
          ├── public          //靜態(tài)訪問目錄 (非必需,可更改)
          │  └── static       //css image文件目錄 (非必需,可更改)
          ├── node_modules    //nodejs模塊目錄
          ├── server.js       //應(yīng)用入口文件 (必需,可更改)
          └── package.json    //npm package.json
          

          應(yīng)用入口

          // server.js
          const {app} = require('iijs');
          
          app.listen(3000, '127.0.0.1', function(err){
              if(!err) console.log('http server is ready on 3000');
          });
          

          Hello world !

          // app/controller/index.js
          class Index {
              constructor(ctx, next) {
                  this.ctx = ctx;
                  this.next = next;
              }
          
              async hello() {
                  this.ctx.body = `hello iijs, hello world !`;
              }
          }
          
          module.exports = Index;
          

          訪問URL:http://localhost/app/index/hello

          輸出結(jié)果:hello iijs, hello world !

          如果關(guān)閉多應(yīng)用模式,可以省去url中的app

          // config/app.js
          {
              app_multi: false, //是否開啟多應(yīng)用
          }
          

          URL地址變?yōu)椋?code>http://localhost/index/hello

          配置路由文件,可以進(jìn)一步簡(jiǎn)化url訪問

          // config/route.js
          [
              {url: '/hello', path: 'index/hello', method: 'get'}
          ]
          

          URL地址變?yōu)椋?code>http://localhost/hello

          注意:多應(yīng)用模式下,路由配置path參數(shù)需要加上應(yīng)用名字,即app/index/hello

          控制器

          為了方便使用,可以繼承系統(tǒng)控制器

          // app/controller/index.js
          const {Controller} = require('iijs');
          
          class Index extends Controller {
              async index() {
                  await this.fetch();
              }
          }
          
          module.exports = Index;
          

          訪問URL:http://localhost/

          注意:系統(tǒng)會(huì)自動(dòng)定位默認(rèn)應(yīng)用、默認(rèn)控制器、默認(rèn)方法

          控制器fetch方法,會(huì)自動(dòng)渲染當(dāng)前應(yīng)用、控制器、方法對(duì)應(yīng)的模板文件:

          app/view/index/index.htm

          也可以指定模板文件

          await this.fetch('list'); // app/view/index/list.htm
          await this.fetch('article/index'); // app/view/article/index.htm
          await this.fetch('app2/article/index'); // app2/view/article/index.htm
          
          await this.fetch('list.html'); // /list.html
          await this.fetch('app2/article/index/list'); // /app2/article/index/list.htm
          

          注意:當(dāng)fetch參數(shù)字符串包含后綴或者目錄超過3級(jí),將自動(dòng)按照應(yīng)用的根目錄地址獲取模板文件

          當(dāng)fetch,第二個(gè)參數(shù)為true時(shí),會(huì)直接返回渲染后的內(nèi)容

          const html = await this.fetch(null, true);
          

          除了fetch,還有三個(gè)方法

          await this.display(content); //直接內(nèi)容輸出
          await this.load(template); //直接文件輸出
          await this.render(content); //渲染內(nèi)容輸出
          

          控制器模板數(shù)據(jù)賦值讀取

          使用assign方法賦值,data方法讀取

          //賦值模版數(shù)據(jù)
          this.assign(name, value);
          
          //獲取模版數(shù)據(jù),name為空時(shí),獲取所有數(shù)據(jù)
          this.data(name);
          

          在控制器中獲取視圖實(shí)例

          this.view; //視圖實(shí)例
          this.view.art; //art-template模板引擎
          this.view.ejs; //ejs模板引擎
          this.view.md; //markdown-it實(shí)例
          

          注意:系統(tǒng)控制器里的視圖實(shí)例和模板引擎實(shí)例,都是按需懶加載的,可以放心使用,建議應(yīng)用控制器都繼承系統(tǒng)控制器。

          應(yīng)用配置文件

          // config/app.js
          const app = {
              app_debug: true, //調(diào)試模式
              app_multi: true, //是否開啟多應(yīng)用
          
              default_app: 'app', //默認(rèn)應(yīng)用
              default_controller: 'index', //默認(rèn)控制器
              default_action: 'index', //默認(rèn)方法
          
              deny_apps: ['common'], //禁止訪問應(yīng)用
              controller_folder: 'controller', //控制器目錄名
              view_folder: 'view', //模板目錄名
              
              view_engine: 'art', //默認(rèn)模版引擎,內(nèi)置(ejs, art)
              view_depr: '_', //模版文件名分割符,'/'代表二級(jí)目錄
              view_ext: '.htm', //模版后綴
          
              static_dir: './public', //靜態(tài)文件目錄,相對(duì)于應(yīng)用根目錄,為空或false時(shí),關(guān)閉靜態(tài)訪問
          
              koa_body: {} //koa-body配置參數(shù),為false時(shí),關(guān)閉koa-body
          }
          
          module.exports = app;
          

          路由配置文件

          // config/route.js
          route = [
              {url: '/', path: 'app/index/index', method: 'get', type: 'controller'},
              {url: '/hello', path: 'app/index/hello', method: 'all'}
          ];
          
          module.exports = route;
          

          注意:?jiǎn)螒?yīng)用模式,可以去掉path參數(shù)中的app,例如path: 'index/index',其他可參考koa-router

          method參數(shù):'get', 'put', 'post', 'patch', 'delete', 'del'

          type參數(shù)為任意自定義的目錄名,controller和view名字可以在app.js配置文件中更改

          案例:路由到應(yīng)用2

          // config/route.js
          {url: '/hello', path: 'app2/index/hello', method: 'get'}
          
          // 執(zhí)行文件app2/controller/index.js hello方法
          

          案例:路由到模板(到模板時(shí),會(huì)直接讀取輸出)

          // config/route.js
          {url: '/hello', path: 'app2/index/hello', method: 'get', type: 'view'}
          
          // 直接輸出app2/view/index/hello.htm 模板內(nèi)容
          

          案例:路由到middleware

          // config/route.js
          {url: '/hello', path: 'app2/index/hello', method: 'get', type: 'middleware'}
          
          // 執(zhí)行文件app2/middleware/index.js hello方法
          

          案例:路由到api

          // config/route.js
          {url: '/hello', path: 'app2/index/hello', method: 'post', type: 'api'}
          
          // 執(zhí)行文件app2/api/index.js hello方法
          

          案例:路由輸出hello world !

          // config/route.js
          {url: '/hello', path: async (ctx, next) => {
              ctx.body = 'hello iijs, hello world !';
          }, method: 'get'}
          
          // 輸出hello iijs, hello world !
          

          全局參數(shù)

          除了koa ctx參數(shù)外,本框架,添加4個(gè)根參數(shù)

          ctx.$app //當(dāng)前請(qǐng)求應(yīng)用名
          ctx.$controller //當(dāng)前請(qǐng)求控制器名
          ctx.$action //當(dāng)前請(qǐng)求方法名
          
          ctx.$ii //應(yīng)用根自動(dòng)懶加載器,相對(duì)應(yīng)用根目錄,可以自動(dòng)加載任意的nodejs模塊,如果模塊是個(gè)class類,可以自動(dòng)實(shí)例化,并傳入ctx next參數(shù),具體可參考npm noader 模塊
          

          事實(shí)上應(yīng)用的控制器方法執(zhí)行用的就是ctx.$ii

          //系統(tǒng)控制器方法執(zhí)行
          await ctx.$ii[ctx.$app][type][ctx.$controller][ctx.$action]();
          
          //執(zhí)行l(wèi)ist控制器index方法
          ctx.$ii.app.controller.list.index();
          //或者
          const list new ctx.$ii.app.controller.list(ctx, next);
          await list.index();
          
          //獲取配置文件
          const cfg_app = ctx.$ii.config.app;
          const cfg_db = ctx.$ii.config.db;
          

          系統(tǒng)helper模塊

          module.exports = {
              isFileSync,
              isDirSync,
              readFile,
              ii: require('noader')
          };
          

          helper.ii為自動(dòng)加載模塊,可以自己實(shí)例化使用,具體用法參考noader模塊。

          瀏覽 15
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          編輯 分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          編輯 分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  99午夜影院 | 国产精品九九九九九九九九九 | 日韩欧美18 | 五月丁香一区 | 男女拍拍拍 |