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 🙏

© 2025 – Pkg Stats / Ryan Hefner

korrect

v0.3.1

Published

a restful framework based on koajs

Readme

build restfull-api server with koa

努力让它成为一个可以复用的框架.

Korrect, this is its name.

目录结构

|-core
|-model
|---schema
|-public
|---image
|---lib
|---layout
|-views
|-app.js
  • core 放置了可以复用的东西, 本框架的基础内容
  • model 定义数据模型, 实例化/prototype扩展baseModel. 详见model 数据模型
  • public 静态资源存放点, image/lib/layout
  • views 视图文件存放

example

how to run: download zip, unzip, use nodejs 0.11 version, and run start cmd:

// nvm, a node version manager
// switch to 0.11 version
nvm use 0.11
cd cur/path
// start cmd
npm start

install and use

install:

npm install korrect

use: create an app.js file, write this:

var appCreator = require('korrect');
var configObj = {db: {...}};
// pass options
var app = appCreator({
    config: configObj,
    model: /abs/path/to/model/folder,
    views: /abs/path/views,
    assets: /abs/path/assets
});
var port = 3004;
app.listen(port, function(){
    console.log('Korrect app is listening at: ', port);
});

config中传递数据库连接参数,model views assets指向相对的目录.

model 数据模型

通过实例化/prototype扩展baseModel生成一个新的model, 示例代码见example/full-demo:

schemaObj很重要, 你可以在里面定义model的所有属性, 各个属性的类型和是否必须. 还可以定义CRUD中哪些方法可以提供. 以user为例

| action | url | 含义 | model方法名 | :------| :--------------: | :---------------------:| -------------: | get | /api/user/:id | 获取某个 | model.read | get | /api/user?foo=bar | 通过query查询,获取一些(query为空时获取所有) | model.readByQuery | post | /api/user | 新建一个用户 | model._create | put | /api/user/:id | 修改一个用户, updates放在request body中 | model.update | put | /api/user?foo=bar | 通过query查询,修改一个用户 | model.update | delete | /api/user/:id | 删除某个用户 | model._delete | delete | /api/user?foo=bar | 通过query查询, 删除某些用户(query为空时会删除所有) | model._deleteByQuery

:id是在schema中制定的关键属性(如自定义的id或者email), 如果没有指定的话会使用mongoDB的默认的_id

由于patch这个http action用的太少, 暂时不计入rest中, update的更新默认为局部更新, 即只会更新updates中传递的字段

上述方法会在每一个model中实现, 可以通过kModel这个全局变量访问kModel.modelName.method(param, [field]), 路由也会自动绑定. 如果对于现有的路由不满意, 可以在schema中配置说明不添加某些路由.然后自己去添加路由, 也可覆盖默认的model方法.

| model方法名 | 参数 | 返回 | :----- | :--: | :----- | | | | | | | | | | | | | | | | | | | | |

以上是初步设定, 后期会根据实际情况进行变更

遵守上述规则, 只写几句配置, 不写一行代码就可以实现restfull api了.

global

Korrect会将几个常用的方法和对象设定为全局变量

// 视图渲染方法:
global.kRender
// 生成的所有model
global.kModel