npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@blued-core/http-server-base

v0.6.4

Published

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

Downloads

35

Readme

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

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

使用方式

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类型。

// 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,也可以选择自行实现。

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 子类。

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 子类。

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')
  },
})