node-faas-express

0.0.7 • Public • Published

OpenFaaS Node.js and Express.js template

Trying the template

$ faas template pull git@gitlab.aibee.cn: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

Readme

Keywords

Package Sidebar

Install

npm i node-faas-express

Weekly Downloads

0

Version

0.0.7

License

ISC

Unpacked Size

34.7 kB

Total Files

19

Last publish

Collaborators

  • npm
  • hujingn