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

v1.0.4

Published

a simple encapsulation for koa2

Downloads

8

Readme

koa-next

a simple encapsulation for koa2, you can use it to build your minimal web server.

npm version

installation

$ npm install koa-next --save

make sure your node version > 7.6.0

start a server

const koa = require('koa-next');
const path = require('path');
 
koa({
  watch: true, // to make auto-reload, default false
  base: __dirname, // base dir
  path: {
    middleware: path.join(__dirname, 'middleware'), // middleware folder
    controller: path.join(__dirname, 'controller'), // controller folder
    service: path.join(__dirname, 'service') //service folder
  }
}).start();

what are middleware controller & service ?

The diff between koa-next & koa2 is koa-next uses three-type handlers to make our coding happier.

middleware

just the same as the koa's middleware

every request will pass all the middlewares set here. u can add logger, login and other handle request funs here. ex,

module.exports = async function m1 (ctx, next) {
  logger.info(`path is ${ctx.path}`);
  await next();
}

every request will log due to this middleware. of course, it must under the middleware folder.

controller

you can set your handlers to handle specific path request, ex, suppose below is the profile.js under controller folder.

module.exports = [
  {
    method: 'get',
    url: '/profile/:name',
    controller: async (ctx) => {
      ctx.body = `u are visiting ${ctx.params.name} home page`;
    }
  },
  {
    method: 'post',
    url: '/profile/annoymous',
    controller: async (ctx) => {
      ctx.body = `not found this person`;
    }
  }
];

when u visit /profile/pony, u can get 'u are visiting pony home page' on your browser, and visit '/profile/annoymous', 'not found this person' comes.

koa-next will pass the params at ctx.params, just as :name above.

Each fragment of the url, delimited by a /, can have the following signature:

  • string - ex /post

  • string|string - | separated strings, ex /post|page

  • :name - Wildcard route matched to a name

  • (regex) - A regular expression match without saving the parameter (not recommended)

  • :name(regex) - Named regular expression match

service

you can set it to add fun at ctx. ex,

module.exports = function (ctx) {
  return {
    json(data) {
      ctx.type = 'application/json';
      ctx.body = JSON.stringify(data, null, 2);
    }
  };
};

suppose the fileName of this code is simpleResponse.js under the service folder.

u can use this at your controller, ex,

  {
    url: '/profile/annoymous',
    controller: async (ctx) => {
      await ctx.simpleResponse.json({user: 'annoymous'});
    }
  }

when u are visting /profile/annoymous, u will set the json type response quickly.

tips

  1. every time you modify your services, middlewares and controllers, koa-next will auto-reload without restart, have fun.

  2. u can add $config.js under middleware folder to custom your middlewares' invoke order, ex,

module.exports = {
  order: ['m2.js', 'm1.js'],
  except: ['m4.js']
}

the m2.js will be ahead of m1.js at every request, and m4.js will be not invoked.

  1. u can set specific middlewares for one controller, add middlewares property at your controller. ex, if u wanna to get a body content from POST method, u can add koa-body middleware at this controller.
{
  url: '/',
  method: post,
  middlewares: requre('koa-body')({formidable:{uploadDir: __dirname}}), 
  controller: async (ctx) => {
    console.log(ctx.request.body);
  }
}

these middlewares only impact this controller.

  1. u can also add middlewares at your config, ex,

koa-next({
  middleware | middlewares: Function | Function <Array> 
})
  1. u can use handlebars at your service like example.

tl;dr

see /example to start it.

see also

koa2

routington

trie

koa-body