Serverless Serve Plugin

When developing with Serverless deploying functions to AWS after each change might be annoying. This plugin allows you to simulate API Gateway locally, so all function calls can be done on localhost.

Installation

In your Serverless project:

npm install serverless-serve

Then in s-project.json add following entry to plugins array:

"serverless-serve"

E.g. like this:

  plugins: ["serverless-serve"]

And in main project root do:

sls serve start

Options

--prefix -p: Add prefix to the URLs, so your clients will not use http://localhost:1465/ but http://localhost:1465/prefix/ instead. Default: empty

--port -P: Port to listen on. Default: 1465

--stage -s & --region -r: If both are given (or there is only 1 region and stage is given), then endpoint response definition will get the template/variable resolved.

--init -i: Initialization file, for custom initializations. Default: empty. NOTE: please specify relative paths from project root folder, e.g. sls serve start -i ./lib/my_init.js. Init file should export single function taking following parameters:

  • Serverless object
  • app object from Express (e.g. to register new routes)
  • handlers object being map of all function names to info about their respective handlers. Since all handlers are require'd lazily, this plugin exports only path information about handler, in following format:
{
  path: "path/to/be/required",
  handler: "exported-function-name"
}

so this should work: require( handlers[ myFunName ].path )[ handlers[ myFunName ].handler ]( event, context )

Example:

module.exports = function(S, app, handlers){
}

Usage

Just send your requests to http://localhost:1465/ as it would be API Gateway.

Using of this plugin with some tool like Nodemon is advised, so Serverless with restart and reload your local code after every change.

Usage with Babel

Optionaly, your handlers can be required with babel-register. To do so, in your s-project.json file, set options to be passed to babel-register like this:

{
  /* ... */
  "custom": {
    "serverless-serve": {
      "babelOptions": {
        /* Your own options, example: */
        presets: ["es2015", "stage-2"]
      }
    }
  },
  "plugins": ["serverless-serve", /* ... */]
}

To view the full list of babel-register options, click here

Simulation quality

This plugin simulates API Gateway for many practical purposes, good enough for development - but is not a perfect simulator. Specifically, no timeout or memory limits are enforced. Mapping templates and/or error codes are not simulated, either. So are security checks. You will probably find other differences.



Serverless Serve Plugin

在每次更改之后,将无服务器部署功能开发到AWS可能会令人烦恼。此插件允许您在本地模拟API网关,因此所有功能调用都可以在localhost上完成。

Installation

在您的无服务器项目中:

npm install serverless-serve

然后在 s-project.json 中添加以下条目到 plugins 数组:

"serverless-serve"
例如。像这样:

  plugins: ["serverless-serve"]

在主项目根目录中:

sls serve start

Options

- 前缀 -p :向URL添加前缀,因此您的客户端将不会使用 http:// localhost:1465 / 而是 http:// localhost:1465 / prefix / 。默认值:空

- port -P :要侦听的端口。默认值: 1465

- stage -s & - region -r :如果同时给出(或者只给出1个区域和阶段),则端点响应定义将得到模板/变量的解析。 / p>

- init -i :初始化文件,用于自定义初始化。默认值:空。注意:请指定项目根文件夹的相对路径,例如 sls服务start -i ./lib/my_init.js 。初始化文件应导出单个函数,采用以下参数:

  • 无服务器对象
  • 来自Express的
  • app 对象(例如注册新路线)
  • 处理程序对象是所有函数名的映射关于它们各自处理程序的信息。由于所有处理程序都是 require ‘d懒惰,此插件仅导出有关处理程序的路径信息,格式如下:
{
  path: "path/to/be/required",
  handler: "exported-function-name"
}

所以这应该工作: require(handlers [myFunName] .path)[handlers [myFunName] .handler](event,context)

示例:

module.exports = function(S, app, handlers){
}

Usage

只需将您的请求发送到 http:// localhost:1465 / ,就像API Gateway一样。

建议使用这个插件与Nodemon这样的工具,所以Serverless重新启动并在每次更改后重新加载你的本地代码。

Usage with Babel

可选,您的处理程序可能需要使用 babel-register 。 为此,在您的 s-project.json 文件中,设置要传递给babel-register的选项,如下所示:

{
  /* … /
  "custom": {
    "serverless-serve": {
      "babelOptions": {
        / Your own options, example: /
        presets: ["es2015", "stage-2"]
      }
    }
  },
  "plugins": ["serverless-serve", / … */]
}

要查看babel-register选项的完整列表,请单击此处

Simulation quality

此插件为许多实际目的模拟API网关,对于开发来说足够好 - 但不是一个完美的模拟器。具体来说,不执行超时或内存限制。映射模板和/或错误代码也不会被模拟。安全检查也是如此。您可能会发现其他差异。




相关问题推荐