# Maty.js 简介

### 安装

```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();
```
