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

polix

v0.1.2

Published

Node.js Web Framework

Downloads

22

Readme

Polix

Node.js Web Framework

Travis Node Version npm

polix是基于koa v2.5.0的装饰器、插件式开发框架,和平常的Node.js Web Framework相比,它无需另外绑定路由集合、可拓展、开发简单,依照java的著名依赖注入框架spring来制作,让开发者专注于逻辑。polix采用多服务多进程架构来保证服务的稳定和快速响应能力。polix的中间件和koa v2.x的中间件保持兼容。polix提供Dockerfile+docker-compose.yml方案进行部署,默认使用的ORMsequelize(后续会提供polix-orm)。开发者可以选择ES6/7/8 或者 TypeScript来进行开发。

以上部分功能尚在开发阶段,敬请关注!

Install

NPM

$ npm i polix --save

Getting Started

使用polix-cli初始化应用

$ npm i polix-cli -g
$ pol init example
$ cd example
$ make build
$ make run-dev

Service

service文件夹下添加user.js

const { Service } = require('polix');

class UserService extends Service {
  constructor(){
    super();
    this._name = {};
  }

  async addUser(userId,name){
    this._name[userId] = name;
    return this;
  }

  async getUser(userId){
    return this._name[userId];
  }
}

module.exports = UserService;

Controller

controller文件夹下添加user.js

const { Controller, GET, POST, DEL, PUT  } = require('polix');

class UserController extends Controller {
  
  // POST /user/addUser
  @POST
  async addUser(param, ctx){
    const {body} = param;
    await this.service.user.addUser(body.userId, body.name);
    ctx.body = {
      result: 'ok'
    };
  }

  // GET /user/getUser
  @GET
  async getUser(param, ctx){
    const {query} = param;
    let user = await this.service.user.getUser(query.userId);
    ctx.body = {
      user
    };
  }

  // GET /user/info
  @GET('info')
  async getInfo(param, ctx){
    ctx.body = {
      v: 'v1.0'
    }
  }

  // PUT /user/updateUser
  @PUT
  async updateUser(param, ctx){
    ctx.body = {
      status: true
    }
  }

  // DEL /user/delUser
  @DEL
  async delUser(param, ctx){
    ctx.body = {
      status: true
    };
  }

  // GET /user/status/:userId
  @GET('status/:userId')
  async getStatus(param, ctx){
    const {router} = param;
    ctx.body = {
      status: true,
      userId: router.userId
    };
  }

}

module.exports = UserController;

Middware

polix的中间件与koa 2.x 的中间件保持兼容
框架默认加载koa-body中间件,如需另外添加中间件则新建middware文件夹(与controller文件夹平级)
添加跨域中间件 ,新建cors.js:

# cors.js

const cors = require('koa2-cors');
module.exports = function(){
  return cors({
    origin: function(ctx) {
      return '*';
    },
    exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'],
    maxAge: 5,
    credentials: true,
    allowMethods: ['GET', 'POST', 'DELETE'],
    allowHeaders: ['Content-Type', 'Authorization', 'Accept']
  });
}

该文件夹下必须存在index.js文件作为总输出中间件文件,加载时根据导出对象的顺序进行绑定中间件

# index.js

const cors = require('./cors');

module.exports = {
    cors // 必须是函数 ,绑定方式为:app.use(cors())
}

Plugin

$ npm i --save polix-request

在项目根目录下的config文件夹下的plugin.default.js中添加以下代码

// `curl`最终会挂载到`this.app`下
exports.curl = {
  // 表示是否启用该插件
  enable: true,
  // 插件`npm`包名
  package: 'polix-request'
};

controller里用polix-request

  @GET
  async getWebInfo(param, ctx){
    let result = await this.app.curl.get('https://www.baidu.com');
    ctx.body = {
      data: result
    }
  }

polix已经内置polix-request插件了,这里只是个演示

Start

$ make dev

Author

Polix © Ricky 泽阳, Released under the MIT License.