微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

iijs 特点使用应用结构应用入口Hello world !控制器应用配置文件路由配置文件全局参数系统helper模块 简单轻量级 MVC 框架

程序名称:iijs 特点使用应用结构应用入口Hello world !控制器应用配置文件路由配置文件全局参数系统helper模块

授权协议: MIT

操作系统: 跨平台

开发语言: JavaScript

iijs 特点使用应用结构应用入口Hello world !控制器应用配置文件路由配置文件全局参数系统helper模块 介绍

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

特点

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

使用

@H_404_10@
  • 安装 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-router

    method参数:’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模块。

    iijs 特点使用应用结构应用入口Hello world !控制器应用配置文件路由配置文件全局参数系统helper模块 官网

    https://js.i-i.me/

    版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

    相关推荐