JSPM

  • Created
  • Published
  • Downloads 26
  • Score
    100M100P100Q68957F
  • License MIT

Export node.js applications into serverless compatible functions

Package Exports

  • @stormkit/serverless
  • @stormkit/serverless/dist/serverless.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@stormkit/serverless) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Stormkit Serverless

Export node.js applications into serverless compatible functions


Stormkit servesless provides handy wrappers to make your code work in serverless environments. This makes your function much more portable. The wrapper will take the incoming event and transform it into a Node.js http.IncomingMessage object.

For whom is this package targeted for?

This repository is intended for users who care about portability. If you know you'll be using AWS Lambda and you don't need to make your code portable, then this package may be overengineering for your needs. If, however, you'd like to deploy your application to multiple providers, then this package can be helpful.

Installation

npm i @stormkit/serverless

Example usage

import http from "http";
import serverless from "@stormkit/serverless";

export const handler = serverless(
  async (req: http.IncomingMessage, res: http.ServerResponse) => {
    res.write("Hello from " + req.url);
    res.end();
  }
);

By default the above example will use a Stormkit handler. To change this behaviour you can tell the serverless function which handler to use.

export const handler = serverless(
  async (req: http.IncomingMessage, res: http.ServerResponse) => {
    res.write("Hello from " + req.url);
    res.end();
  },
  "awsAlb"
);

To avoid setting the handler type all the time, you can also use the process.env.SERVERLESS_HANDLER environment variable to set a different default type. Allowed types are:

  • awsAlb
  • stormkit

If you need a different handler, feel free to open a issue 🙏🏻

Currently supported signatures

Each provider has it's own signature to call a serverless function. Here's the list of the currently supported providers:

Testing locally

It is possible to test this repository locally by using the dev-server.

  1. Open a terminal
  2. Go to your project
  3. Build your project (usually npm run build)
  4. Clone @stormkit/serverless (where you clone is unimportant)
  5. Run npm install
  6. Run REPO_PATH=<local-path-to-your-repo> npm run dev

Currently nuxt and next projects are detected and configured automatically. If you're testing other frameworks, you can submit a feature request.

Alternatively, you can install @stormkit/serverless and run the local server directly in your repository with programmatic usage:

import DevServer from "@stormkit/serverless/dist/dev-server"

new DevServer({
  host: "localhost",
  port: 3000,
  dir: "path/to/api/folder"
}).listen()

See dev-server for the available options.

License

Made with 💛 Published under MIT.