# @blued-core/http-server-base

> 基于 `routing-controller` 的简易封装，抽出了一些复用的逻辑。

Latest version **0.6.5** (published 2025-04-27) · ISC license · 0 weekly downloads

## Install

```sh
npm install @blued-core/http-server-base
pnpm add @blued-core/http-server-base
yarn add @blued-core/http-server-base
bun add @blued-core/http-server-base
```

## Health

**Score 25/100 (F)** — status: maintenance-mode.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.6.5 |
| Published | 2025-04-27 |
| First published | 2019-04-24 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 8 |
| Unpacked size | 29.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | jiasm, ryli, nieweidong, fengcang2020, jarvan8888, chisecc |

## Links

- npm: https://www.npmjs.com/package/@blued-core/http-server-base
- npm.io page: https://npm.io/package/@blued-core/http-server-base

## Dependencies (8)

- [koa](https://npm.io/package/koa.md) ^2.7.0
- [koa-router](https://npm.io/package/koa-router.md) ^7.4.0
- [koa-static](https://npm.io/package/koa-static.md) ^5.0.0
- [koa-bodyparser](https://npm.io/package/koa-bodyparser.md) ^4.2.1
- [reflect-metadata](https://npm.io/package/reflect-metadata.md) ^0.1.13
- [routing-controllers](https://npm.io/package/routing-controllers.md) ^0.7.7
- [@blued-core/client-intl](https://npm.io/package/@blued-core/client-intl.md) ^0.1.0
- [@blued-core/logger-intl](https://npm.io/package/@blued-core/logger-intl.md) ^0.1.3

## Recent versions

- 0.6.5 (latest) — 2025-04-27
- 0.6.4 — 2024-02-27
- 0.6.3 — 2024-02-20
- 0.6.2 — 2024-02-06
- 0.6.1 — 2024-02-06
- 0.6.0 — 2024-02-05
- 0.5.0 — 2023-03-21
- 0.4.0 — 2021-08-12
- 0.3.2 — 2021-03-30
- 0.3.1 — 2020-09-29
- 0.3.0 — 2020-09-29
- 0.2.3 — 2020-07-15
- 0.2.2 — 2020-03-09
- 0.2.1 — 2019-12-08
- 0.2.0 — 2019-07-08
- … 9 more at https://npm.io/package/@blued-core/http-server-base/versions

## README

基于 `routing-controller` 的简易封装，抽出了一些复用的逻辑。  

```bash
npx install-peerdeps @blued-core/http-server-base
```

## 使用方式

```typescript
import { createServer } from '@blued-core/http-server-base'

// 最简易的用法
createServer()
```

## 提供的参数

option|type|default|desc
:--|:--|:--|:--
port|`number`|`8000`|设置服务的端口号
isLocal|`boolean`|false|是否为本地开发环境，部分插件本地环境下会有额外的操作
distPath|`string`|-|静态资源路径
controllersPath|`string|string[]`|项目根目录下的`controllers`文件夹|设置路由文件存储的位置
controllerPattern|`string|string[]`|`['.ts', '.js']`|设置路由文件匹配的后缀
middlewareRegsiter|`Function`|-|自定义的中间件注册方式，会传递`Koa`实例到回调中
success|`Function`|-|服务启动成功后的回调
errorOverriding|`Object`|`ParamRequiredError`|自定义的异常对象覆盖
loggerClient|`Client`|-|日志插件
errorReportClient|`() => Client`|-|异常监控插件
performanceClient|`() => Client`|-|性能监控插件
before|Function|-|前置的全局中间件处理
after|Function|-|后置的全局中间件处理

### errorOverriding 结构描述

用于覆盖部分`routing-controllers`的自定义`Error`类型。  

```typescript
// key 为匹配异常类型对应的 name 所需
ParamRequiredError: {
  // 用于返回值的输出
  message: "missing field",
  // 用于设置覆盖后的异常类型
  name: "RequiredError",
  // 用于设置 http response status code
  statusCode: 403,
  // 用于在 JSON 类型的数据返回值中设置 code
  errorCode: 403,
}
```

### loggerClient

logger 使用的是符合 `@blued-core/logger-intl` 约束的子类实现。  
默认提供了一个 `@blued-core/winston-logger`，也可以选择自行实现。  

```typescript
import { createServer } from '@blued-core/http-server-base'
import LoggerClient from '@blued-core/winston-logger'
import Cache from '@blued-core/cache'

const cache = new Cache()
// 加载 log
const loggerClient = new LoggerClient(logPath, cache, isLocal)

createServer({
  loggerClient,
})
```

### errorReportClient

用于错误监控时添加数据上报，使用的为符合 `@blued-core/raven-client#RavenClientBuilder` 的结构。  
默认提供了一个`@blued-core/raven-client`， 也可以自行实现 `@blued-core/client` 子类。  

```typescript
import { createServer } from '@blued-core/http-server-base'
import { NormalConf } from '@blued-core/normal-conf'
import RavenClient from '@blued-core/raven-client'
import Cache from '@blued-core/cache'

const cache = new Cache()

const normalConf = new NormalConf({
  raven: 'XXX',
})
// 加载 raven
const ravenClient = new RavenClient(normalConf, cache)
ravenClient.isLocal = isLocal

createServer({
  errorReportClient () {
    return ravenClient.getClient('raven')
  },
})
```

### performanceClient

用于性能监控时添加数据上报，使用的为符合 `@blued-core/statsd-client#StatsdClientBuilder` 的结构。  
默认提供了一个`@blued-core/statsd-client`， 也可以自行实现 `@blued-core/client` 子类。  

```typescript
import { createServer } from '@blued-core/http-server-base'
import { NormalConf } from '@blued-core/normal-conf'
import StatsdClient from '@blued-core/statsd-client'
import Cache from '@blued-core/cache'

const cache = new Cache()

const normalConf = new NormalConf({
  statsd: {
    // statsd 上报 IP
    conf: '0.0.0.0',
    // statsd 上报 IP 对应的端口
    port: 1234,
    // 项目归属分组
    group: 'demo-group',
    // 项目名
    project: 'demo-project',
  },
})

const statsdClient = new StatsdClient(normalConf, cache)
statsdClient.isLocal = isLocal

createServer({
  performanceClient () {
    return statsdClient.getClient('statsd')
  },
})
```

---
_Source: https://npm.io/package/@blued-core/http-server-base · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
