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

node-faas-express

v0.0.7

Published

node express faas template

Downloads

3

Readme

OpenFaaS Node.js and Express.js template

Trying the template

$ faas template pull [email protected]:dataplatform/node-faas-template.git --overwrite
$ faas new function --lang node-express
# 由于路径问题,固定项目名叫function,会生成function目录,接口后缀手动修改function.yml文件的functions
# 由于node模块问题,需在外层安装package.json,copy配置文件
$ cp template/node-express/package.json template/node-express/babel.config.js template/node-express/.eslintrc.js template/node-express/.gitignore ./

Start Development

$ npm install
# 修改 function/utils/config.yml数据库等配置
# 自动生成表模型
$ npm run model 表名
# 启动服务
$ npm start
$ npm stop

build && push && deploy

# 修改 function.yml文件
# test-faas-node 为部署到openfaas后接口后缀 http://openfaas.aibee.cn/function/test-faas-node
provider:
  name: openfaas
  gateway: http://openfaas.aibee.cn
functions:
  test-faas-node:
    lang: node-express
    handler: ./function
    image: registry.aibee.cn/bi-faas-web/test-faas-node:0.0.1

# 执行
faas-cli build -f ./function.yml
faas-cli push -f ./function.yml
faas-cli deploy -f ./function.yml

Config

function/utils/config.yml

env:
  host: localhost:3000
  prefix: ""
schedule:
  # 定时任务,每10秒执行方法 service.projects.test
  projects.test: "*/10 * * * *"

Example usage

Example with express HTTP API:

/**
 * /function/routes/label_types.js
 * app express router
 * db sequelize and all models
 * util wrap tools
 **/
export default function(app, db, util, service) {
  app.get("/label_types", function(req, res) {
    db.label_types.findAll().then(data => {
      res.json(util.wrap(data));
    });
  });

  app.post("/label_types", function(req, res) {
    let checkData = util.checkModelData(req.body, db.label_types);
    if (checkData) {
      res.json(util.wrapError(checkData));
      return;
    }
    db.label_types
      .create(req.body)
      .then(data => {
        res.json(data);
      })
      .catch(err => {
        res.json({ err: err });
      });
  });
}

Service Methods:

/**
 * /function/service/label_types.js
 * db sequelize and all models
 * all service
 **/
export default function(db, service) {
  return service.define("projects", {
    /**
     * 更新项目配置,同时更新日常编排任务配置
     * @param {*} pid
     * @param {*} params
     */
    async updateProjectWithTask(pid, params) {
      let label_configuration = params.label_configuration || {};
      return await db.sequelize.transaction(t => {
        return Promise.all([
          db.projects.update(params, {
            where: { id: pid },
            transaction: t
          }),
          db.daily_tasks.update(
            {
              assigned_group_list: label_configuration.assigned_group_list,
              max_worker_count: label_configuration.max_worker_count
            },
            {
              where: { project_id: pid },
              transaction: t
            }
          )
        ]);
      });
    },
    async test(){
      console.log("schedule doing");
    }
  });
}

Example with sequelize models:

// generate by initModels.js 
/* jshint indent: 1 */

module.exports = function(sequelize, DataTypes) {
  return sequelize.define(
    "label_types",
    {
      id: {
        type: DataTypes.INTEGER(11),
        allowNull: false,
        primaryKey: true,
        autoIncrement: true
      },
      name: {
        type: DataTypes.STRING(128),
        allowNull: false
      },
      type: {
        type: DataTypes.STRING(128),
        allowNull: false,
        unique: true
      },
      label_configuration: {
        type: DataTypes.TEXT,
        allowNull: true
      },
      created_at: {
        type: DataTypes.DATE,
        allowNull: true,
        defaultValue: sequelize.literal("CURRENT_TIMESTAMP")
      },
      updated_at: {
        type: DataTypes.DATE,
        allowNull: false,
        defaultValue: sequelize.literal("CURRENT_TIMESTAMP")
      }
    },
    {
      tableName: "label_types",
      timestamps: false
    }
  );
};

swagger support:

  /**
   * @swagger
   * definitions:
   *   projects:
   *     properties:
   *       category_id:
   *         type: string
   *         description: 分类ID
   *       name:
   *         type: string
   *         description: 项目名称
   *       status:
   *         type: integer
   *         description: 项目状态
   */

  /**
   * @swagger
   * /projects:
   *   get:
   *     tags:
   *       - 项目管理
   *     description: 分页查询
   *     produces:
   *       - application/json
   *     parameters:
   *       - name: name
   *         description: 项目名称
   *         in: query
   *         type: string
   *       - name: page
   *         in: query
   *         type: integer
   *       - name: size
   *         in: query
   *         type: integer
   *     responses:
   *       200:
   *         description: An array of projects
   *         schema:
   *            $ref: '#/definitions/projects'
   */

Use Technology && Help Docs

express web服务框架 express

sequelize 数据库操作ORM框架 sequelize / sequelize blog

swagger 接口文档生成器 swagger / swagger example

start with node10-express template