iijs- 简单轻量级 MVC 框架

网友投稿 982 2022-10-20

iijs- 简单轻量级 MVC 框架

iijs- 简单轻量级 MVC 框架

iijs是一个基于nodejs+koa2构建的简单轻量级MVC框架,最低依赖仅仅koa和koa-router。

特点

本MVC框架极为轻量小巧,又自由灵活,使用简单,功能又足够强大,可开发简单的页面展示网站,可以开发pai接口应用,也可支撑复杂的多应用网站。

使用

安装 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.jsconst {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.jsclass 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.jsconst {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.htmawait this.fetch('article/index'); // app/view/article/index.htmawait this.fetch('app2/article/index'); // app2/view/article/index.htmawait this.fetch('list.html'); // /list.htmlawait 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.jsconst 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.jsroute = [ {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模块。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:基于springboot的城市公园信息管理系统的设计与实现-计算机毕业设计
下一篇:关于feign调用的参数传递问题(@RequestBody和@RequestParam)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~