heidloff.net - Building is my Passion
Post
Cancel

Developing Serverless Functions with TypeScript

One of the coolest capabilities of Apache OpenWhisk is the ability to develop functions with Docker. This allows you to develop functions in languages which are not supported out of the box by the platform.

I’ve open sourced a sample that shows how to develop and debug functions with TypeScript. I’m a big fan of TypeScript since it adds a type system to JavaScript which makes me more productive.

Get the code from GitHub.

Here is a very simple TypeScript function. The /run endpoint is where the actual implementation of the function goes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import * as express from 'express';
import * as bodyParser from 'body-parser';

const app = express()
app.use(bodyParser.json());

app.post('/run', (req, res) => {
    var payload = (req.body || {}).value;

    var result = { 
        "result": {
            "echo": payload
        }
    }
    res.status(200).json(result);
});

app.post('/init', function (req, res) {
    try {
        res.status(200).send();
    }
    catch (e) {
        res.status(500).send();
    }
});

app.listen(8080, () => console.log('Listening on port 8080'))

Based on this recipe I’ve also documented how you can debug TypeScript code running in a Docker container from Visual Studio Code. In order to debug TypeScript code, the same mechanism is used which I explain in this video. A volume is used to share the files between the IDE and the container and VS Code attaches a remote debugger to the Docker container. The functions can be changed in the IDE without having to restart the container. nodemon restarts the Node application in the container automatically when files change.

This is a screenshot of the debugger in VS Code.

image

If you want to try out OpenWhisk in the cloud, you can get an account on the IBM Cloud.

Featured Blog Posts
Disclaimer
The postings on this site are my own and don’t necessarily represent IBM’s positions, strategies or opinions.
Trending Tags