iijs-一个基于nodejskoa2构建的简单轻量级MVC框架

6次阅读

共计 4543 个字符,预计需要花费 12 分钟才能阅读完成。

iijs

A simple and lightweight MVC framework built on nodejs+koa2

项目介绍

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

官网:js.i-i.me 源码:github 码云 QQ:331406669

使用

  1. 安装 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 模块

特点

本 MVC 框架极为轻量小巧,又自由灵活,使用简单,功能又足够强大,可开发简单的页面展示网站,可以开发 pai 接口应用,也可支撑复杂的多应用网站。你有什么意见或者好的建议,欢迎交流探讨。

License

MIT

最后

虽说是 MVC 框架,却没有 M,待下个版本,我再添加。

正文完
 0