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-framework-wlxh

v1.0.3

Published

往来翕忽nodejs 服务专门定制的egg 框架,包含了:amqplib、consul、redis、mysql、sequelize、middleware(httpAuth)

Downloads

6

Readme

egg-framework-wlxh

往来翕忽nodejs 服务专门定制的egg 框架,包含了:amqplib、consul、redis、mysql、sequelize、middleware(httpAuth)

egg loader egg-remote-config

QuickStart

$ npm install
$ npm test

publish your framework to npm, then change app's framework config:

// {app_root}/index.js
{
  "name": "egg-framework-wlxh",
  "egg": {
    "framework": "egg-framework-wlxh"
  }
}

包含插件:egg-sequelize、egg-validate、egg-path-matching、amqplib、consul、egg-redis

插件开关

// config.*.js
connect: {
  sequelize: true,
  redis: true,
  rabbitmq: true,
},

consul 配置

consul 默认关闭

如果不实用consul:

// config.*.js
// consul 服务发现配置
consul: {
  connect: true, // 是否初始化连接Consul, 默认true
  fetchConfig: true, // 是否获取consul的配置 true
  configKey: 'nodejs/parking-server', // 配置中心key
  server: {
    host: '127.0.0.1', // 注册中心ip地址
    port: 8500, // 注册中心端口号
  },
  register: true, // 是否注册当前模块,默认为true
  id: `${dockerId}:nodejs-parking-server`, // id (String, optional): check ID
  // serviceid (String, optional): service ID, associate check with existing service
  // http (String): url to test, 2xx passes, 429 warns, and all others fail
  name: 'nodejs-parking-server', // name (String): check name
  address: ip, // address (String, optional): service IP address
  port: localPort, // port (Integer, optional): service port
  // meta (Object, optional): metadata linked to the service instance
  tags: [ 'development', 'nodejs' ], // tags (String[], optional): service tags
  check: {
    http: `http://${ip}:${localPort}/consul/health`, // 健康检测地址
    interval: '5s', // 健康检测间隔
    notes: 'http service check',
    status: 'critical', // status (String, optional): initial service status
  },
  services: [
    // // 服务发现列表
    // {
    //   serviceName: 'merchant-admin-aggregator', // 服务Name
    //   referName: 'merchant-admin-aggregator', // 引用名,后续可用 app.services.referName 访问服务
    //   comment: '实时收费列表', // 备注
    //   // address: '', // 服务的地址
    // },
    {
      serviceName: 'common-service', // 服务Name
      referName: 'common-service', // 引用名,后续可用 app.services.referName 访问服务
      comment: '操作员相关', // 备注
      // address: '', // 服务的地址
    },
  ],
},

‼️健康检测地址 consul/health, router、controller 需要自己在项目中添加

// app/controller/consul.js
'use strict';

const { Controller } = require('egg');

class ConsulController extends Controller {
  async health() {
    const { ctx } = this;
    ctx.body = {
      code: 200,
      msg: 'success',
      timestamp: Date.now(),
    };
  }
}

module.exports = ConsulController;

// app/router.js
router.get('/consul/health', controller.consul.health);

amqplib 配置

// config.*.js
{
  rabbitmq: {
    protocol: 'amqp',
    host: '127.0.0.1',
    port: 5672,
    username: 'rabbit',
    password: '**********',
    heartbeat: 2000,
  }
}
// config/mq.js
// 配置rabbitmq consumer,producers此处配置暂时没有用到

'use strict';

module.exports = {
  producers: [
    {
      exchange: 'eggmqproducer.exchange.message',
      exchangeType: 'topic',
    },
  ],
  consumers: [
    {
      exchange: 'eggmqproducer.exchange.message',
      exchangeType: 'topic',
      queue: 'eggmqconsumer.queue.textMessage',
      topic: 'text.*',
      consumer: 'foo.bar',
    },
  ],
};

// 业务代码中使用producer
const msg = JSON.stringify({
  name: 'lucy',
  age: 12,
});
const exchangeName = 'abc_dbe'; // 交换机名
const exchangeType = 'direct'; // exchangeType
const routingKey = 'people'; // 路由密钥

app.messenger.send('@@egg-mq/producer', {
  exchange: exchangeName,
  routingKey,
  exchangeType, // 可以不传,默认:direct
  payload: msg,
});

redis 配置

// config.*.js
{
  redis: {
    client: {
      host: '127.0.0.1',
      port: 6379,
      password: '*******',
      db: 0,
    },
  },
}

egg-sequelize 配置

详细配置请查看egg-sequelize

// config.*.js
{
  sequelize: {
    // delegate: 'model', // load all models to app.model and ctx.model
    // baseDir: 'model', // load models from `app/model/*.js`
    dialect: 'mysql',
    host: '127.0.0.1',
    port: 13306,
    database: 'user',
    username: 'root',
    password: '**********',
    timezone: '+08:00',
  },
}

中间件

httpAuth

httpAuth 配置

使用 egg-path-matching 匹配路由

// config.*.js
const pathMatching = require('egg-path-matching');

{
  middleware: [ 'httpAuth' ],
  httpAuth: {
    tokenKey: 'session.token', // 必需的
    match: pathMatching({
      match: [ /^\/nodejs\/parking/ ],
    }),
  },
}

const user = ctx.user // 获取用户信息

扩展

helper

helper

parseRedisRes

转化redis 获得的内容,json 字符串转化为对象

ctx.helper.parseRedisRes(await app.redis.get(content))

stringifyRedisRes

转化js 对象为字符串,存入redis

await app.redis.setex(key, 60 * 60 * 24 * 7, ctx.helper.stringifyRedisRes(content));

插件

egg-validate

自定义校验规则

  • jsonString:json string
  • string|number: string or number

Questions & Suggestions

Please open an issue here.

change log

  • 1.0.2 发送mq 消息 durable 参数可配置,默认值true
  • 1.0.3 修复getServiceFromConsul 获取可用服务失败