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

egg-router-factory

v1.0.2

Published

An egg plugin that automatically generates routes based on path and configuration

Downloads

10

Readme

egg-router-factory

node version NPM version build status Test coverage David deps npm download

This plugin provides the ability to read files from a specified directory and create routes based on the contents of the files. It can also easily load and configure different middleware by writing your own RouterFactory class, and simply generate API documents.

中文说明

Install

$ npm i egg-router-factory --save

Usage

  1. Create the required files
app
+-- http
|   +-- get
|   |   +--index.js
|   |   +--yourpage.js
|   +-- post
|       +--api
|          +--yourapi.js
+-- factory.js
  1. Enable plugin
// {app_root}/config/plugin.js
exports.routerFactory = {
  enable: true,
  package: 'egg-router-factory',
};
  1. Write your own factory class. In most cases, we only need to inherit RouterFactory and override the setMiddlewares and buildDoc methods.
// {app_root}/app/factory.js
'use strict';
const RouterFactory = require('egg-router-factory');

class MyFactory extends RouterFactory {
  // Add middleware one by one according to the configuration
  setMiddlewares(obj, args) {
    const app = this.app;
    if(obj.params) {
      args.push(app.middlewares.ajvValidate(obj.params));
    }
    if(obj.userauth) {
      args.push(app.middlewares.userauth(obj.userauth));
    }
    args.push(app.middlewares.onError);
  }
  // Generate documents based on route configuration
  buildDoc() {
    let text = '';
    for (let i = 0; i < this.routers.length; i++) {
      const obj = this.routers[i];
      text += `[${obj.method}]${obj.item.path || obj.path}\n`;
      if(obj.item.params){
        text += params2doc(obj.item.params);
      }
      if(obj.item.userauth){
        text += userauth2doc(obj.item.userauth);
      }
    }
    return text;
  }
}

module.exports = MyFactory;
  1. Write your router files
// {app_root}/app/http/get/index.js
'use strict';

module.exports = {
  path: '/',
  controller: 'home.index',
}
// {app_root}/app/http/get/yourpage.js
module.exports = app => {
  params: { /* some params configuration */ },
  controller: app.controller.home.yourpage,
}
// {app_root}/app/http/post/api/yourapi.js
module.exports = () => {
  params: { /* some params configuration */ },
  userauth: { /* some auth configuration */ },
  async controller() {
    /* do something here */
    this.body = 'success';
  },
}
  1. Run buildAllRouters in router.js
// {app_root}/app/router.js
'use strict';
module.exports = app => {
  app.routerFactory.buildAllRouters(app.router);
};
  1. Complete! You can also call buildDoc to generate documentation in test cases or elsewhere if needed.

Router file

Router configuration file supports two formats

  • Export a function containing the unique parameter app, which returns a configuration JSON
  • Export a configuration JSON directly

Router file default properties

| property | required | description | |:---------|:---------|:------------| | controller | true | It can be a string corresponding to the controller, a controller function, or a non-parameter asynchronous function. When it is an asynchronous function with no arguments, the this pointer points to the current ctx. | | name | false | Route name, can be omitted | | path | false | Route path, the default is to use the relative path of the configuration file as the route path. |

Configuration

see config/config.default.js for more detail.

Unit tests

npm test

License

MIT This README was translate by google