Building and Distributing NodeJS 12.x Express Serverless Application With Lyrid
By: Handoyo Sutanto
Tags: Image Processing, Web Development, Lyrid Tutorials, Serverless, Nodejs

Introduction

This is a series of tutorials that I will start building mainly focusing on showcasing how things work and how to build an application using Lyrid. This example specifically will show you how to deliver a standard NodeJS 12.x running Express Web Server as a Lyrid application.

Although these blogs are about our own technology, you will soon see that there is nothing in this platform that requires you to conform with a non-standard method of building an application. Everything you do here can still carry forward to writing an application on your own Virtual Machine. For instance, Docker Container, or Kubernetes.

Prerequisites

A laptop or PC (Mac/Windows/Ubuntu don’t really matter)

NodeJS 12.x or above installed: https://nodejs.org/en/download/

npm: https://www.npmjs.com/get-npm

Lyrid account: you can register here for free, no credit card required, along with Lyrid CLI downloaded.

Git: https://git-scm.com/book/en/v2/Getting-Started-Installing-Git

Any of your favorite IDE, I use Visual Studio Code in this example.

Some HTTP testing tool (I will use Insomnia in this example, but either one works):

Postman: https://www.postman.com/downloads/ or

Insomnia: https://insomnia.rest/download/

Initial Setup

First you will need to register your Lyrid account:

Register Here To Start Lyrid

And go through the 3 sections:

This whole guide assumes that you already registered with Lyrid, downloaded our command line client, and initialized the client with your access and secret key.

You will then need to clone the base project. For this example, it is hosted at GitHub:

Local Build and Test

To run the sample locally, simply run these commands:

cd lyrid-expressjs

npm install

npm start

Simple…so far so good…lets dig into what we have in the code. This sample REST API is quite simple:

It contains a basic index.js where it will initialize an express web server application instance, and the function entry point (entry.js) will define how this instance is built along the routes. There’s a logging middleware (morgan: http://expressjs.com/en/resources/middleware/morgan.html) that logs every request for this application.

This service has two routes:

/echo/* : This endpoint will just echo all your request url, and method then prints it out as a text response. Test it with Insomnia by passing HTTP POST Method and /echo/hello_world request:

Looks good!

/sharp/ : We will get into this endpoint in the later portion of the tutorial.

Lyrid Definition File and Entry Point

Let’s explain a bit about this GitHub project example. The structure of the code is similar to our initial code when you run:

lc code init --name "$AppName" --module "$ModuleName" --function "$FunctionName" --language "nodejs12.x" --web "express"

Except in this case we already prefilled all those names and description.

A few things to explain in this template is the .lyrid-definition.yml file, and inside it there is an entry point called entry.js

Changing this definition file is not required, but it is crucial to explain how this file is being used by our platform.

The definition file contains descriptors that will be used in our platform to identify how the function will be built and packed. In this scenario, because it is an Express Project, our build will expect an express server instance application to be returned from the entry point (entry.js). The user has the flexibility to modify the entry point to include more routes and middleware inside the project, and our platform will take in the express server instance and wrap the instance for different types of serverless platforms.

The definition file determines how your function will be called inside our API Gateway.

NOTES: This is specific for our NodeJS 12.x Support. Our current support for NodeJS and Express is only for non-asynchronous function execution, and continues until the event loop is empty or the function times out. We will have support for asynchronous Promise based type of wrapper soon.

Public Cloud Deployment

Now your code is working locally, the next question is: how to make this API run on a publicly accessible endpoint?

Run our submission command from the folder that contains our definition file:

lc code submit

In this stage, your function will be zipped, and wrapped by our platform and the platform will perform a build to create a build artifact for all the public cloud serverless platforms that we support.

At the end of the submission, save the endpoint generated by the platform.

In this stage, we went through a standard serverless function build. This runs the pre-built installation using npm install and packages the function as an application.

Run the following command to monitor your build output:

lc monitor -c '*.build.*'

When your build is deployed to your default execution platform, your function is published and ready to use.

Public Cloud Execution

Now that your application is deployed, execute it using our endpoint. Remember the structure of our endpoint is currently: