> For the complete documentation index, see [llms.txt](https://tofishes.gitbook.io/maty-js/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tofishes.gitbook.io/maty-js/master.md).

# Maty.js 简介

Maty是一个基于Koa的使用简单且友好的页面渲染服务框架。Maty使用双洋葱圈模型，配置好路由信息（接口地址及数据处理方法等），即可方便的搭建一个项目服务。

### 安装

```bash
$ npm install maty
```

{% hint style="info" %}
&#x20;Maty.js requires **node v8.0** or higher
{% endhint %}

### 功能

* Router config 简单配置即可实现接口数据的获取及页面渲染，甚至接口代理等
* Filters request/response 过滤器，可以自定义请求的处理和响应，扩展功能
* Interceptors 拦截器，可用作公共请求处理等
* ctx.forward 服务器端跳转，例如同一个路由地址，可以根据不同的参数转到不同的页面
* emplate engine 默认使用[nunjucks](https://mozilla.github.io/nunjucks/)模板引擎，支持多引擎同时使用
* Auto render 在无路由配置的情况下，自动渲染和请求路径一致的模板文件

### 工作流程示意图

![maty workflow](https://981705929-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LPTmUgelNJEttb937_V%2F-LPz2631EUPsY6j3PJuX%2F-LPz45l3Zs7wpbekKCt-%2Fmaty-js-worker.png?alt=media\&token=66ba0f6c-b21a-4084-945f-75b49cb9866e)

使用代码描述工作流程，如下：

```javascript
const app = maty();

// middleware
app.use(async (ctx, next) => {
  await next();
});

// filter
app.filter('request', async (ctx, next) => {
  // before request handler
  await next();
  // after request handler
});

// filter
app.filter('response', async (ctx, next) => {
  // before response handler
  await next();
  // after response handler
});

// middleware
app.use(async (ctx, next) => {
  await next();
});

app.listen();
```
