iijs
A simple and lightweight MVC framework built on nodejs+koa2
項目介紹
一個基於nodejs+koa2構建的簡單輕量級MVC框架,最低依賴僅僅koa和koa-router
官網:js.i-i.me 源碼:github 碼雲 QQ:331406669
使用
- 安裝
npm i iijs
應用結構
├── app //應用目錄 (非必需,可更改)
│ ├── Controller //控制器目錄 (非必需,可更改)
│ │ └── index.js //控制器
│ ├── view //模板目錄 (非必需,可更改)
│ │ └── index //index控制器模板目錄 (非必需,可更改)
│ │ └── index.htm //模板
│ ├── model //模型目錄 (非必需,可更改)
│ ├── logic //邏輯目錄 (非必需,可更改)
│ └── **** //其他目錄 (非必需,可更改)
├── app2 //應用2目錄 (非必需,可更改)
├── common //公共應用目錄 (非必需,可更改)
├── config //配置目錄 (非必需,不可更改)
│ ├── app.js //APP配置 (非必需,不可更改)
│ ├── route.js //路由配置 (非必需,不可更改)
│ └── **** //其他配置 (非必需,可更改)
├── public //靜態訪問目錄 (非必需,可更改)
│ └── static //css image文件目錄 (非必需,可更改)
├── node_modules //nodejs模塊目錄
├── server.js //應用入口文件 (必需,可更改)
└── package.json //npm package.json
應用入口
// 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
輸出結果:hello iijs, hello world !
如果關閉多應用模式,可以省去url中的app
// config/app.js
{
app_multi: false, //是否開啓多應用
}
URL地址變爲:http://localhost/index/hello
配置路由文件,可以進一步簡化url訪問
// config/route.js
[
{url: '/hello', path: 'index/hello', method: 'get'}
]
URL地址變爲:http://localhost/hello
注意:多應用模式下,路由配置path參數需要加上應用名字,即app/index/hello
控制器
爲了方便使用,可以繼承系統控制器
// app/controller/index.js
const {Controller} = require('iijs');
class Index extends Controller {
async index() {
await this.fetch();
}
}
module.exports = Index;
訪問URL:http://localhost/
注意:系統會自動定位默認應用、默認控制器、默認方法
控制器fetch方法,會自動渲染當前應用、控制器、方法對應的模板文件:
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
注意:當fetch參數字符串包含後綴或者目錄超過3級,將自動按照應用的根目錄地址獲取模板文件
當fetch,第二個參數爲true時,會直接返回渲染後的內容
const html = await this.fetch(null, true);
除了fetch,還有三個方法
await this.display(content); //直接內容輸出
await this.load(template); //直接文件輸出
await this.render(content); //渲染內容輸出
控制器模板數據賦值讀取
使用assign方法賦值,data方法讀取
//賦值模版數據
this.assign(name, value);
//獲取模版數據,name爲空時,獲取所有數據
this.data(name);
在控制器中獲取視圖實例
this.view; //視圖實例
this.view.art; //art-template模板引擎
this.view.ejs; //ejs模板引擎
this.view.md; //markdown-it實例
注意:系統控制器裏的視圖實例和模板引擎實例,都是按需懶加載的,可以放心使用,建議應用控制器都繼承系統控制器。
應用配置文件
// config/app.js
const app = {
app_debug: true, //調試模式
app_multi: true, //是否開啓多應用
default_app: 'app', //默認應用
default_controller: 'index', //默認控制器
default_action: 'index', //默認方法
deny_apps: ['common'], //禁止訪問應用
controller_folder: 'controller', //控制器目錄名
view_folder: 'view', //模板目錄名
view_engine: 'art', //默認模版引擎,內置(ejs, art)
view_depr: '_', //模版文件名分割符,'/'代表二級目錄
view_ext: '.htm', //模版後綴
static_dir: './public', //靜態文件目錄,相對於應用根目錄,爲空或false時,關閉靜態訪問
koa_body: {} //koa-body配置參數,爲false時,關閉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;
注意:單應用模式,可以去掉path參數中的app,例如path: 'index/index',其他可參考koa-routermethod參數:'get', 'put', 'post', 'patch', 'delete', 'del'
type參數爲任意自定義的目錄名,controller和view名字可以在app.js配置文件中更改
案例:路由到應用2
// config/route.js
{url: '/hello', path: 'app2/index/hello', method: 'get'}
// 執行文件app2/controller/index.js hello方法
案例:路由到模板(到模板時,會直接讀取輸出)
// config/route.js
{url: '/hello', path: 'app2/index/hello', method: 'get', type: 'view'}
// 直接輸出app2/view/index/hello.htm 模板內容
案例:路由到middleware
// config/route.js
{url: '/hello', path: 'app2/index/hello', method: 'get', type: 'middleware'}
// 執行文件app2/middleware/index.js hello方法
案例:路由到api
// config/route.js
{url: '/hello', path: 'app2/index/hello', method: 'post', type: 'api'}
// 執行文件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 !
全局參數
除了koa ctx參數外,本框架,添加4個根參數
ctx.$app //當前請求應用名
ctx.$controller //當前請求控制器名
ctx.$action //當前請求方法名
ctx.$ii //應用根自動懶加載器,相對應用根目錄,可以自動加載任意的nodejs模塊,如果模塊是個class類,可以自動實例化,並傳入ctx next參數,具體可參考npm noader 模塊
事實上應用的控制器方法執行用的就是ctx.$ii
//系統控制器方法執行
await ctx.$ii[ctx.$app][type][ctx.$controller][ctx.$action]();
//執行list控制器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;
系統helper模塊
module.exports = {
isFileSync,
isDirSync,
readFile,
ii: require('noader')
};
helper.ii爲自動加載模塊,可以自己實例化使用,具體用法參考noader模塊
特點
本MVC框架極爲輕量小巧,又自由靈活,使用簡單,功能又足夠強大,可開發簡單的頁面展示網站,可以開發pai接口應用,也可支撐複雜的多應用網站。你有什麼意見或者好的建議,歡迎交流探討。
License
最後
雖說是MVC框架,卻沒有M,待下個版本,我再添加。