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

koa-restful

v1.0.10

Published

koa-restful模块

Downloads

13

Readme

Build restfull-api server with koa

Install

koa-restful is available using npm:

npm install koa-restful

example

use : create koa app.js file ,require koa-restful and write this

目录结构

|-core
|-model
|---schema
|-public
|---image
|---lib
|---layout
|-views    
|-app.js

代码

var app = require('koa')(),
	restful = require('koa-restful');
var opt = { 
    modelPath : '../../app/models',
    controllerPath : '../../app/controllers',
    config : {
    	db : ''
    }
};
if( opt.modelPath ){
    restful(opt.modelPath, opt.controllerPath, opt.config.db, app);
}
else{
    log('app-init', 'no model folder, did not init models and models-routes');
}
app.listen(3000);
  • modelPath : model文件夹路径
  • controllerPath : controllerPath 文件夹路径
  • config : 数据库等配置信息

model 数据模型

通过实例化/prototype扩展baseModel生成一个新的model:

schemaObj很重要, 你可以在里面定义model的所有属性, 各个属性的类型和是否必须. 以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), 如果没有指定的话会使用默认的_id 上述方法会在每一个controller中实现, 可以通过$$controllers_o这个全局变量访问$$controllers_o.controllerName.method(param, [field]), 路由也会自动绑定. 如果对于现有的路由不满意, 可以在schema中配置说明不添加某些路由disable默认为false即添加.然后自己去添加路由, 也可覆盖默认的model方法.

MIT Licensed