Deploy a Node.js Function

Last updated about 2 months ago

Run this function locally

See for best practices and instructions on setting up your development environment to work on Koyeb.

Run the function

At the moment, we use Docker to run a function locally. We plan to improve and package the local development environment soon to provide a better experience. To run your function locally type:

1docker run -p 8080:8080 --rm  -e KOYEB_HANDLER=.handler -e KOYEB_HANDLER=hello-world.handler -v $PWD/hello-world:/var/task -ti koyeb/runtime:nodejs14 --debug

This command launches a Docker container using the koyeb/runtime:nodejs14 image (providing a NodeJS version 14 runtime) and listening on the port 8080.

Invoke the function

We use CloudEvents for describing event data in standard formats to provide interoperability across services, platforms, and systems. To invoke the function, you need to pass required HTTP headers to mock the event handled by the function:

  • ce-source: A string representing the event source.
  • ce-type: A string representing the event type.
  • ce-subject: A string representing the event subject.
  • ce-specversion: The CloudEvents spec version
  • ce-id: A string representing the event ID. For instance to invoke a function with a basic payload, run the following command:
1 curl localhost:8080  -H 'content-type: application/json' -H "ce-specversion: 1.0" -H "ce-source: local-invokation" -H "ce-type: dev" -H "ce-subject: local-function-invokation" -H "ce-id: 1" -d '{"body": "Hello World!"}'

In the function definition, you will receive the body in the event parameters and the event details in context:

1const handler = async (event, context, callback) => {
2    // your code here.
4module.exports.handler = handler;

The callback parameter is optional but recommended. Your handler should use the callback to return either an error (as the first parameter) or a response object.