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

circe

v1.0.8

Published

A framework based on Koa v2.

Downloads

12

Readme

Circe

A framework based on Koa v2.

基于Koa v2的开发框架。

关于发音

英语:Circe[ˈsə:si]

中文:瑟茜[sè xī]

一、安装

$ npm install circe --save

二、入门

基于Koa2开发,保留与之相同的用法,所有api和中间件均兼容

// Koa
const Koa = require('koa')
const app = new Koa()
app.use(/* 中间件 */)
app.listen(8080. function () {/* 回调 */})

// Circe
const Circe = require('circe')
const circe = new Circe()
circe.use(/* 中间件 */)
circe.listen(8080. function () {/* 回调 */})

Koa实例对象的属性都有(具体的作用请查看Koa官方文档):

  • use
  • toJSON
  • inspect
  • on
  • proxy
  • middleware
  • subdomainOffset
  • env
  • context
  • request
  • response
  • keys

三、进阶

1. 可获得Koa实例和http server实例

const Circe = require('circe')
const circe = new Circe()

// koa实例
console.log(circe.app)

// server实例,可用于其他框架的结合,如socket.io
console.log(circe.server)

2. 更多的实例方法

circe.route(Router) 注册路由,传入路由对象

const Circe = require('circe')
const router = new Circe.Router()

router.get('/user', async () => { /* 中间件 */ })
router.post('/user', async () => { /* 中间件 */ })

circe.route(router)

circe.route(String) 注册路由,传入路由目录

//////// apis/user.js ////////////////////
const Circe = require('circe')
const router = new Circe.Router()

router.get('/user', async () => { /* 中间件 */ })
router.post('/user', async () => { /* 中间件 */ })

module.exports = router

//////// app.js ////////////////////
const path = require('path')
const Circe = require('circe')

// 将导入apis目录下的所有路由文件
circe.route(path.resolve(__dirname, 'apis'))

circe.inject(key, value) 注入内容到context,传入键和值参数

const Circe = require('circe')
const circe = new Circe()

circe.inject('$hello', function () { console.log('hello') })
circe.inject('$a.b', 'test' })
circe.inject('foo.bar', {a: 1, b: 2})

circe.use(asynct (ctx, next) => {
  ctx.$hello() // 打印'hello'
  ctx.$a.b // 'test'
  ctx.foo.bar.a // 1
  ctx.foo.bar.b // 2
})

circe.inject(object) 注入内容到context,传入对象和前缀

const Circe = require('circe')
const circe = new Circe()

circe.inject({a: 1, b: 2})
circe.inject({$c: 3, $d: 4})

circe.use(asynct (ctx, next) => {
  ctx.a // 1
  ctx.b // 2
  ctx.$c // 3
  ctx.$d // 4
})

3. 内置中间件全家桶

内置的中间件全都绑定在Circe类上,不需要再去npm或github上需找和对比需要的中间件,更多的中间件正在丰富中。

4. 拓展的context

除了koa自带的context方法和属性,circe对context进行了拓展:

  • ctx.success(data[, code]) 成功响应,详细文档查看responseApis
  • ctx.fail(msg[, code]) 错误响应,详细文档查看responseApis

5. 实用工具库

config 配置文件管理

const Circe = require('circe')
// 根据环境变量,读取目录下配置文件,支持多级目录
const config = Circe.config.from(__dirname + '/config')

示例1:

  • config
    • default.js
    • development.js
    • production.js

如有以上目录结构,且当前运行环境为development,将会整合default.jsdevelopment.js作为导出对象。

示例2

  • config
    • default
      • app.js
    • development
      • app.js
      • db.js
    • production
      • app.js
      • db.js
      • other.js

如有以上目录结构,且当前运行环境为production,将会整合defaultproduction目录下所有文件作为导出对象。

导出对象结构为:

{
  app: {...},
  db: {...},
  other: {...}
}

6. 支持typescript

已添加对typescript的声明,详情请看index.d.ts

import * as Circe from 'circe'

const circe = new Circe()

模板项目

circe-template - circe + babel circe-template-ts - circe + typescript