Skip to content

Latest commit

 

History

History
90 lines (54 loc) · 2.47 KB

File metadata and controls

90 lines (54 loc) · 2.47 KB

Use typescript with Node runtime

Requirements

This example assumes you are familiar with how serverless functions work. If needed, you can check Scaleway's official documentation

This example uses the Scaleway Serverless Framework Plugin. Please set up your environment with the requirements stated in the Scaleway Serverless Framework Plugin before trying out the example.

Finally, you will need Node.js installed in your computer to run this example.

Context

By default, Node runtime treats files with the .js suffix. If you wish to use Typescript language with Node runtime, you can do so by following this example.

Description

This example aims to show how to use Typescript language with Node runtime (node 18 runtime in this example). Used packages are specified in package.json.

The function in this example returns a simple "Hello world!" with a status code 200.

Setup

Install npm modules

Once your environment is set up (see Requirements), you can install npm dependencies from package.json file using:

npm install

Install a Typescript compiler

Then, it is necessary to install the Typescript compiler package globally.

npm install -g typescript

You can run tsc --version to ensure the compiler is correctly installed.

Create a Typescript configuration file

When this is done, you can initialize the Typescript project with Node.js. For that, you can run:

tsc --init

This will create a tsconfig.json file in the project root directory.

Transpile your code

Before deploying your function, you need to transpile your Typescript code into brower readable JavaScript.

tsc

Test locally

The last step before deploying your function is to test it locally. For that, you can run:

NODE_ENV=test node handler.js

This will launch a local server, allowing you to test the function. In another terminal, you can now run:

curl -X GET http://localhost:8080

The expected output is "Hello world!".

Deploy and run

Finally, if the test succeeded, you can deploy your function with:

serverless deploy

Then, from the given URL, you can check the result in a browser or by running the following command:

# Get request
curl -i -X GET <function URL>

The output should be "Hello world!".