1.1.6 • Published 1 year ago

quick-koa-server v1.1.6

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

quick-koa-server

快速构建 koa 服务

功能概览

  • 对 quick-koa-server 构建的服务配置跨域(拦截 OPTIONS 请求定向放行)
    • 支持同时配置多个跨域
    • 支持对常见跨域内容项进行单独配置
  • 内置前端渲染 historyApiFallback
  • 支持对前端根页面(/index)注入环境变量(基于 koa-ejs)
  • 静态资源访问服务
  • 支持自由扩展中间件
  • 配置生成 node 本地服务器
    • 支持同时配置多个服务目录、路由
    • 自动生成路由,支持扫描深层目录
    • 自动过滤特殊目录、文件,仅保留有效 router 导出
  • 配置生成网关服务
    • 支持根据环境变量转发指定目标地址
    • 支持对请求头进行配置化放行,默认拦截所有请求头信息
    • 支持自定义 timeout,默认 6e4
    • 支持转发上传类接口
    • 支持转发下载类接口
  • 支持配置健康检查请求

配置介绍

  • koaStaticOpt koa-static 配置
  • koaEjsOpt koa-ejs 配置
  • koaBodyOpt koa-body 配置
  • CORSOpts 跨域配置
  • getwayOpt 网关配置,支持 REST 接口
  • apiOpts node 服务配置
  • passHeaders 允许透传的请求头,除常用 headers 外默认拦截所有请求头
  • processEnvKey 在 process.env 中获取环境变量的 key,默认 'DEPLOY_ENV'
  • timeout 请求超时时间(单位毫秒),默认 6e4
  • healthPath 健康检查请求地址(一般用于校验 node 服务是否可用),默认 '/health'

使用示例

import QServer from 'quick-koa-server';

const app = QServer({
  koaStaticOpt: { root: '/dist' },
  koaEjsOpt: { root: '/dist', layout: 'index', viewExt: 'html', cache: false, debug: false },
  CORSOpts: [{ origin: 'xxx.com', headers: ['cookie'] }],
  getwayOpt: {
    '/api/xxx': {
      target: 'xxx.com',
      pathRewrite: { '^/api/xxx': '' },
    },
  },
  apiOpts: [{ path: '/api/qserver/xxx', dir: '/xxx' }],
  passHeaders: ['cookie'],
});

app.listen(8080, () => {
  console.log('Success: Ready at http://localhost:8080');
});

自由扩展本地路由示例

import * as path from 'path';
import * as Router from 'koa-router';
import { getApiRouter } from 'quick-koa-server/lib/routes/api';

const router = new Router();
getApiRouter(router, {
  path: '/api/qserver/factory',
  dir: path.resolve(__dirname, './factory/api'),
});

app.use(router.routes());
1.1.1

1 year ago

1.1.6

1 year ago

1.1.5

1 year ago

1.1.4

1 year ago

1.1.3

1 year ago

1.1.2

1 year ago

1.1.0

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago