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-pattern

v1.0.0

Published

pattern middleware for koa, a koa middleware that support mvc and rest

Downloads

2

Readme

koa-pattern

Build status NPM version NPM downloads Dependency Status License

A Really Simple koa middleware that support mvc and rest , including template rendering middleware.

Note: This middleware is for koa@2. koa@1 test not recovery.

Installation

$ npm install koa-pattern
$ npm run example

Templating engines

koa-pattern is using consolidate for veiw render under the hood.

List of supported engines

Example

var pattern = require('koa-pattern');

app.use(pattern({
        dir: __dirname,
        prefix: '',     // url prefix or virtual directory
        mvcPrefix: 'pages',//if undefine or '',null,  not run mvc  pattern
        restPrefix: 'api',//if undefine or '',null,  not run rest pattern
        //if have used others view middleware or template Engine, and has ctx.render method, can not config tmplEngine
        tmplEngine: 'dot'//or {name:'dot', 'extension':'html'}
    }));
  • opts.dir: Where your ctrls & views are located. this location must direct include ctrls/views, Must be an absolute path.
  • opts.prefix (optional) url prefix or virtual directory
  • opts.mvcPrefix: url mvc prefix
  • opts.restPrefix: url rest prefix
  • opts.tmplEngine: template Engine name string or object include name and extension: {name:'dot', 'extension':'html'}

For more examples you can take a look at the example.

Demo Dir

example
    |-ctrls
    |   |-user.js
    |-views
    |    |-user
    |        |-index.html
    |        |-list.html
    |-index.js

mvc pattern HTTP GET http://localhost/pages/ctrlName/actionName

eg: HTTP GET http://localhost/pages/user/list It will render views dir user/list.html automatic, and view's data return by ctrls/user.js list function

if not ctrlName or actionName, it default 'index', and if not index dir, root view default

//ctrls/user.js
module.exports.list = (ctx, next) => {
  return [{
      userName: 'bd',
      userNo: '007'
  },{
      userName: 'bd2',
      userNo: '9527'
  }]
}

rest pattern HTTP GET/POST/PUT/DELETE http://localhost/restPrefix/resourceName/[respration]

Rest Pattern support 2 url style to render api json data eg:

  1. HTTP GET/POST/PUT/DELETE http://localhost/api/user/007
  2. HTTP GET/POST http://localhost/api/user/list

actually, 1 and 2 is just the same rule url

and the 1 will render json by match HTTP method matched function, and 007 will be url argument, eg 007 is userid, if not HTTP method matched function, it will match ctrls/user.js 007/list methods, match rule

    'GET': 'query',
    'POST': 'update',
    'PUT': 'create',
    'DELETE': 'remove'

so url 1 return list as below, url2 if HTTP METHOD IS GET will match query return

module.exports.list = (ctx, next) => {
  return [{
      userName: 'bd',
      userNo: '007'
  },{
      userName: 'bd2',
      userNo: '9527'
  }]
}

module.exports.query = (ctx, next) => {
  return {
      userName: 'bd',
      userNo: '007'
  }
}

rest and mvc pattern async supported

module.exports.asynclist = (ctx, next) => {
    var data = {}
    var kk = () => new Promise((resolve, reject) => {
        setTimeout(function(){
            resolve([{
                userName: 'bd',
                userNo: '007'
            },{
                userName: 'bd2',
                userNo: '9527'
            }])
        },1000)
    })
    return kk().then((data) => {
        return data
    });
}

License

MIT