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

api-fast

v1.2.5

Published

快捷创建node接口

Downloads

66

Readme

快速创建你的node api接口

安装

npm install api-fast --save

1.1.* 更新内容

[1] 添加多个mysql同时请求的方法
[2] 添加多个mysql相互依赖请求

使用方法 1.0.* -- 1.1.*

const { join, normalize, relative, resolve } = require('path')
let server = require('api-fast')
// 载入配置信息
server.config({
    // 项目路径(最好默认resolve('./'))
    root: resolve('./'),
    // 前端文件上传目录
    static: 'files/',
    // 上传文件缓存路径(root目录下自动创建)
    uploadCache: 'tmp',
    // 前端html运行环境目录
    wwwroot: 'webapps',
    // 跨域配置信息
    cors: {
      origin: [
        'https://www.webascii.cn'
      ],
      credentials: true,  // 是否带cookie
      maxAge: '1728000'
    },
    /**
    * 插件目录(root目录下的plugins文件夹)
    * 请参考"@如何写插件"
    */
    plugins: 'plugins',
    /**
    * 接口目录(root目录下的interface文件夹)
    * 请参考"@如何写接口"
    */
    interface: 'interface',
    // mysql信息(如果不想用mysql,可以mysql:false)
    // 不使用mysql不可开启接口config参数 mysql = false
    mysql: {
        host: 'localhost',
        user: 'root',
        password: '123456',
        port: '3306',
        database: 'database_name',
        multipleStatements: true // 允许多条查询
    },
    // 文件上传默认关键字
    uploadKey: 'files',
    // 启动服务端口号
    port: 10086,
    qiniuConfig: {
        // 七牛云自定义域名(你的自定义域名,用于返回七牛云上传后的绝对路径)
        website: '//cdn.webascii.cn/',
        ak: '', // 七牛AccessKey
        sk: '', // 七牛SecretKey
        scope: 'webascii', // 七牛存储空间名称
        /**
        * 机房	Zone对象
        * 华东	qiniu.zone.Zone_z0
        * 华北	qiniu.zone.Zone_z1
        * 华南	qiniu.zone.Zone_z2
        * 北美	qiniu.zone.Zone_na0
        */
        zone: 'Zone_z1', // 七牛空间(默认Zone_z1)
        pathCDN: 'test/', // 上传到CDN的路径
    }
})
// 启动服务
server.run()

如何写插件

在你的项目目录(root)下创建plugins目录
创建你的插件 myPlugin.js
文件的名字建议以驼峰命名,因为你需要通过此名称调用你的插件
--------比如我们写一个返回x+y的插件--------
module.exports = async (x, y) => {
  return x + y
}
这样你的插件就完成了
如何使用? 请参考"#如何创建接口"里的部分内容

如何创建接口

在你的项目目录(root)下创建interface目录
创建你的接口文件 myInterface.js
支持多级文件夹,文件名随意写
/**
 * 配置信息
 */
module.exports.config = {
  path: '/api/demo', // 接口
  method: 'post', // 请求方式
  dir: 'a/b/c', // 文件保存路径(默认default)
  upload: false, // 是否上传文件到本地 默认: false(优先级 < qiniu)
  verify: false, // 是否开启用户验证: 默认false
  qiniu: false, // 是否开启七牛上传: 默认false(优先级 > upload)
  // 此参数可以不填写,以后补充文档
  qiniuConfig: {
      // 优先级 > server.config.qiniuConfig.pathCDN
      pathCDN: 'kyle/demo/'
  },
  mysql: false, // 是否需要mysql 默认: false
}
/**
 * 验证函数
 * @param param 参数
 * @param req
 * @param res
 * @returns {Promise.<void>}
 * return false 验证失败(终止程序)
 * return other 验证成功
 */
module.exports.verify = async ({req, res, apiConfig, app, connection, param, interfaceApi, result, file, plugins}) => {
  // 这里可以添加你的任意验证逻辑
  // 如果验证失败 return false 即可终止程序
}

/**
 * 参数处理函数
 * return {Boolean} false(终止程序)
 * return {Object} 进入生命周期下一步的param
 */
module.exports.param = async ({req, res, apiConfig, app, connection, param, interfaceApi, result, file, plugins}) => {
  // 这里可以添加你的参数验证逻辑
  // 如果验证失败 return false 即可终止程序
}
/**
 * sql语句 是否使用取决于 config.mysql
 * @returns {{sql: string, sqlParam: Array}}
 * return {Object}
 * Object.sql {String} sql语句,多个sql请用';'隔开
 * Object.sqlParams {Array} sql参数,对应Object.sql的缺省值
 */
module.exports.mysql = async ({req, res, apiConfig, app, connection, param, interfaceApi, result, file, plugins}) => {
  // 是否使用此函数,取决于config.mysql
  return {
    sql: 'INSERT INTO demo(id,url_path) VALUES(0,?)',
    sqlParams: [
      'http://www.webascii.cn'
    ]
  }
}
/**
 * 请求成功
 */
module.exports.success = async ({req, res, apiConfig, app, connection, param, interfaceApi, result, file, plugins}) => {
  // 如何使用你的插件(上文我们创建了pluginx/myPlugin.js这个插件)
  let num = plugins.myPlugin(100, 200) // 300
  res.send({
    status: 200,
    data: {
      xy: num
    }
  })
}

访问按照上文创建的接口

请使用post方式请求接口

http://localhost:10086/api/demo

接口参数说明

module.exports.success = async ({
                                  req,
                                  res,
                                  apiConfig,
                                  app,
                                  connection,
                                  param,
                                  interfaceApi,
                                  result,
                                  file,
                                  plugins
}) => {}
// req: 请求参数
// res: 返回参数
// apiConfig: 当前接口实例
// connection: 数据库实例
// param: 用户通过接口传递过来的参数
// interfaceApi: 当前服务实例
// result: mysql返回结果(取决于是否使用mysql)
// file: 文件上传结果(取决于是否上传了文件)
// plugins: 你的自定义插件

参数说明

module.exports.config = {
  path: '/api/demo',
  method: 'post',
  dir: 'a/b/c',
  upload: false,
  verify: false,
  qiniu: false,
  qiniuConfig: {
    // 优先级 > server.config.qiniuConfig.pathCDN
    pathCDN: 'kyle/demo/'
  },
  mysql: false,
}

config.path // 接口地址
config.method // 接口请求方式
config.uploadKey // 覆盖默认的上传字段
config.dir // 文件保存路径(默认default)
config.upload // 是否上传文件到本地 默认: false(优先级 < qiniu)
config.verify // 是否开启用户验证: 默认false
config.qiniu // 是否开启七牛上传: 默认false(优先级 > upload)

/**
* config.qiniuConfig
* [非必填]
* 七牛配置信息 (config.qiniu == true 生效)
*/
config.qiniuConfig

/**
* config.qiniuConfig.pathCDN
* [非必填]
* 更改默认的server.config.qiniuConfig.pathCDN
* 示例:
* pathCDN存在:website/path[config.qiniuConfig.pathCDN]/fileName.png
* pathCDN不存在:website/path[server.config.qiniuConfig]/fileName.png
*/
config.qiniuConfig.pathCDN

/**
* config.qiniuConfig.scope
* [非必填]
* 更改默认的server.config.qiniuConfig.scope
* 示例:
* scope存在:上传scope == config.qiniuConfig.scope
* scope不存在:上传scope == server.config.qiniuConfig.scope
*/
config.qiniuConfig.scope

/**
* config.qiniuConfig.zone
* [非必填]
* 更改默认的server.config.qiniuConfig.zone
* 示例:
* zone存在:上传zone == config.qiniuConfig.zone
* zone不存在:上传zone == server.config.qiniuConfig.zone
*/
config.qiniuConfig.zone

/**
* config.qiniuConfig.website
* [非必填]
* 更改默认的server.config.qiniuConfig.website
* 示例:
* website存在:website[config.qiniuConfig.website]/path/fileName.png
* website不存在:website[server.config.qiniuConfig.website]/path/fileName.png
*/
config.qiniuConfig.website
/**
* config.qiniuConfig.fileHash
* [非必填]
* 是否允许文件结尾带hash值
* 示例:
* false:website/path/ytd6cyg5v6ky/fileName.png
* true:website/path/fileName-ytd6cyg5v6ky.png
*/
config.qiniuConfig.fileHash 

/**
* config.mysql {Boolean}
* [非必填]
* 默认:false
* false: 不开启mysql
* true: 开启mysql 默认执行module.exports.mysql方法
*/
config.mysql
/**
* config.mysql {Array}
* 开启多个mysql同时请求module.exports.mysql01、module.exports.mysql02
*/
config.mysql = ['mysql01', 'mysql02']

/**
* config.mysqlRely
* [非必填]
* 决定当config.mysql == Array时,是同时执行还是顺序执行
* 默认:false
* 解释:
* false: 同时请求module.exports.mysql01、module.exports.mysql02
* true: 先请求module.exports.mysql01得到结果并把结果作为参数(module.exports.mysql02({result}))给到module.exports.mysql02然后执行module.exports.mysql02
*/
config.mysqlRely

生命周期

avatar