iijs-基于nodejs+koa2构建的简单轻量级MVC框架koa-router官网:js.i-i.me 源码:github 码云 QQ:331406669使用npmiiijs应用结构├──app//应用目录(非必填,可改)│├──Controller//控制器目录(非必填,可改)││└──index.js//Controller│├──view//模板目录(非必填,可改)││└──index//索引控制器模板目录(非必填,可改)││└──index.htm//模板│├──model//模型目录(│├──logic//逻辑目录(非必填,可改)│└──****//其他目录(非必填,可改)├──app2//应用2目录(非必填,可改)├──common//普通应用目录(非必填,可改)├──config//配置目录(非必填,不可改)├──app.js//APP配置(非必填,不改)│├──route.js//路由配置(非必填,不可改)│└──****//其他配置(非必填,可改))├──public//静态访问目录(非必填,可改)│└──static//css图片文件目录(非必填,可改)├──node_modules//nodejs模块目录├──server.js//应用入口文件(必填,可改)└──package.json//npmpackage.json应用入口//server.jsconst{app}=require('iijs');app.listen(3000,'127.0.0.1',function(err){if(!err)console.log('httpserverisreadyon3000');});Helloworld!//app/controller/index.jsclassIndex{constructor(ctx,next){this.ctx=ctx;这个.下一个=下一个;}asynchello(){this.ctx.body=`你好iijs,你好世界!`;}}module.exports=索引;访问网址:http://localhost/app/index/hello输出结果:helloiijs,helloworld!如果关闭多应用模式,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注意:在多应用模式下,路由配置路径参数需要加上应用名,即app/index/hellocontroller可以继承systemcontroller,为了方便//app/controller/index.jsconst{Controller}=require('iijs');classIndexextendsController{asyncindex(){awaitthis.fetch();}}module.exports=索引;访问地址:http://localhost/注:系统会自动定位默认应用、默认控制器、默认方法控制器fetch方法,并自动动态渲染当前应用、控制器、方法对应的模板文件:app/view/index/index.htm也可以指定模板文件awaitthis.fetch('list');//app/view/index/list.htmawaitthis.fetch('article/index');//app/view/article/index.htmawaitthis.fetch('app2/article/index');//app2/view/article/index.htmawaitthis.fetch('list.html');///list.htmlawaitthis.fetch('app2/article/index/list');///app2/article/index/list.htm注意:当fetch参数字符串中包含后缀或目录超过3级时,会根据应用的根目录地址自动获取模板文件。fetch的第二个参数为true时,直接返回渲染后的内容。consthtml=awaitthis.fetch(null,true);一共有三种方法awaitthis.display(content);//直接内容输出awaitthis.load(template);//直接文件输出awaitthis.render(content);//rendercontentoutputcontrollertemplatedataassignmentreaduseassignmethodassignvalue,datamethodread//赋值模板数据this.assign(name,value);//获取模板数据,当name为空时,获取所有数据this.data(姓名);在控制器this.view中获取视图实例;//视图实例this.view.art;//art-template模板引擎this.view.ejs;//ejs模板引擎this.view.md;//markdown-it实例注意:系统控制器中的视图实例和模板引擎实例是按需延迟加载的,可以放心使用。建议应用控制器继承系统控制器应用配置文件//config/app.jsconstapp={app_debug:true,//调试模式app_multi:true,//是否开启多应用default_app:'app',//默认应用default_controller:'index',//defaultControllerdefault_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}模块。出口=应用程序;路由配置文件//config/route.jsroute=[{url:'/',path:'app/index/index',method:'get',type:'controller'},{url:'/hello',路径:'app/index/hello',方法:'all'}];module.exports=route;注意:单应用模式下,可以去掉path参数中的app,例如path:'index/index',其他可以参考koa-routermethod参数:'get','put','post','patch','delete','del'类型参数为任意自定义目录名,controller和view名可以在app.js配置文件改变大小写:routetoapplication2//config/route.js{url:'/hello',path:'app2/index/hello',method:'get'}//执行文件app2/controller/index.jshellomethodcase:routetothetemplate(去模板时直接读取输出)//config/route.js{url:'/hello',path:'app2/index/hello',method:'get',type:'view'}//直接输出app2/view/index/hello.htm模板内容示例:路由到中间件//config/route.js{url:'/hello',path:'app2/index/hello',method:'get',type:'middleware'}//执行文件app2/中间件/索引。jshellomethodcase:routetoapi//config/route.js{url:'/hello',path:'app2/index/hello',method:'post',type:'api'}//执行文件app2/api/index.jshello方法示例:路由输出helloworld!//config/route.js{url:'/hello',path:async(ctx,next)=>{ctx.body='helloiijs,helloworld!';},method:'get'}//输出helloiijs,helloworld!本框架除了全局参数的koactx参数外,增加了4个根参数ctx.$app//当前请求应用名ctx.$controller//当前请求控制器名ctx.$action//当前请求方法名ctx.$ii//应用根自动懒加载器,相对于应用根目录,可以自动加载任何nodejs模块,如果模块是类,可以自动实例化,传入ctxnext参数。详情请参考npmnoader模块。其实application的controller方法执行是ctx.$ii//系统controller方法执行awaitctx.$ii[ctx.$app][type][ctx.$controller][ctx.$action]();//执行列表控制器索引方法ctx.$ii.app.controller.list.index();//或constlistnewctx.$ii.app.controller.list(ctx,next);awaitlist.index();//获取配置文件constcfg_app=ctx.$ii.config.app;constcfg_db=ctx.$ii.config.db;系统助手模块module.exports={isFileSync,isDirSync,readFile,ii:require('noader')};helper.ii是一个自动加载模块,可以自己实例化使用。具体用法参考noader模块的特性。这个MVC框架极其轻量、紧凑、自由灵活、简单易用,而且足够强大。可以开发简单的页面展示网站、pai界面应用、复杂的多应用网站。如果您有任何意见或好的建议,欢迎交流讨论。LicenseMIT虽然最后是一个MVC框架,但是没有M,我会在下个版本加上。
