Deploying modern Deno web applications on Serverless

Take advantage of the horizontal expansion and pay-as-you-go advantages of Serverless, combined with the customized runtime, to realize the rapid migration of Web applications. The Deno runtime is a new generation of JS runtime. With the combination of Alibaba Cloud Serverless+Deno, we can quickly develop modern web applications.

With the Serverless Devs tool, Deno can quickly implement code deployment. So far, the combination of Alibaba Cloud functional computing+Serverless Devs+Deno+TypeScript has realized rapid software development, rapid iteration, rapid deployment, rapid scaling, and the use of cloud advantages to quickly solve business problems.

Why Serverless

In the field of Web development, Serverless has the greatest advantage over traditional methods: it can achieve horizontal expansion under the premise of low operation and maintenance. In this way, developers can focus on the business instead of paying too much attention to complicated technical details in order to support the possible high concurrency, because focusing on the business greatly improves the development efficiency and reduces the development cost.

At the same time, the elastic horizontal expansion can support the business needs of any access frequency without worrying about the cost and benefit discrepancy. Applications from 1 PV per day to 1000000 PV per day can be supported, without additional configuration, cluster management, and fixed costs. Pay by usage.

We can understand Serverless as a container cluster that does not need to be managed on demand. In the development of Web applications, Serverless has more advantages in terms of technology and cost.

So, how to realize the migration of traditional Web applications to Serverless?

In theory, any programming language can be used on Serverless. However, in terms of specific implementation, Serverless provides the following methods:

1. Run the script language directly (when the Serverless container has run with the target language, only upload the code)

2. Runtime+language script (all uploaded to Serverless)

3. Executable file (compiled locally as executable file, and then uploaded to Serverless)

4. Custom container (currently Alibaba Cloud Serverless supports Docker image to generate container)

The above methods are supported on Alibaba Cloud Serverless. Compare the advantages and disadvantages of the above methods:

• Running the script language directly is the shortest cold start time (that is, the time to prepare the Serverless container before code execution);

• Custom containers are the best for compatibility, and we use a balanced approach here: custom runtime.

The reason why we choose the custom runtime is that the runtime we use is Deno, a modern JS/TS runtime. On the one hand, although JS is a scripting language, Alibaba Cloud currently does not support the Deno runtime, so it cannot run applications on Serverless by directly running the scripting language.

On the other hand, Deno provides the function of packaging into executable files similar to Go language, which can realize cross-platform running of code without using containers.

Why Deno

Deno is a JS/TS runtime. Like Node.js, it uses V8 as the JS interpreter to ensure speed. Unlike Node.js, it uses Rust instead of C++as the development language to communicate with the operating system. (The other thing that they are the same is that the initiator and the original main developer are Ry, Ryan Dahl)

Node.js is undoubtedly influential and successful when it runs as a backend JS. However, Node.js was born many years ago. With the development of ECMAScript, JavaScript (JS) has changed. In order to ensure ecological compatibility, Node.js is difficult to carry out drastic reform, which is the background of the birth of Deno.

For developers, Deno not only provides better ECMAScript compatibility, but also provides out-of-the-box TypeScript (TS) support, which provides more robust type constraints and greatly reduces unnecessary consumption of development environment configuration.

At present, with the continuous iteration of the Deno version, in the Web development scenario, Deno has been able to achieve most of the functions that Node.js can achieve. Using Deno as the runtime can be used quickly by JS developers without too much burden. Compared with Node.js, Deno has two main advantages:

1. Support Web APIs (such as fetch, crypto, etc.).

2. Support file reference without using package manager like npm.

Web API support can get through the front and back technology stacks, while NPM has been a long time for developers.

Simple Deno Web application case

It is very simple to develop a Web application using Deno. Now let's quickly create a simple Web application to realize UA display.

Create Serverless function

With the code, we also need to deploy the address. Here we take Alibaba Cloud function computing FC as an example. First, create a custom runtime function:

1. Enter Alibaba Cloud function computing web console

2. Click "Services and Functions" in the left navigation bar

3. Click "Create Service" to create the service

4. Click "Service Name" to enter the service interface

5. Click "Create Function"

6. When creating a function, select "Migrate Web Server smoothly using custom runtime", and then create the function

Then you need to bind the domain name:

1. After returning to the function calculation homepage, click "Domain name management" in the left navigation bar

2. Click "Add Custom Domain Name"

3. When adding a custom domain name, select the service and function we just created

At this point, we have completed all the basic configurations and obtained the required parameters:

1. Service name

2. Function name

3. Access domain name

One-click deployment of Deno web application on Serverless

It is very convenient to deploy the Deno Web application on Serverless. We can implement one-click deployment. We can use the customized runtime of Alibaba Cloud Serverless to build an executable file locally, and then deploy the code to the function calculation through the Serverless Devs tool, and then execute it on Serverless.

Here we assume that the Deno and s tools have been installed in the local environment. Now create the following configuration file and put it in/User/zsqk/web/s.yaml.

The code and deployment configuration file are ready. Now you just need to build and upload the code.

First, enter the/User/zsqk/web/directory, and use the following to run the build and upload commands using Deno:

//Build the deno file
const r = Deno.run({
cmd: [
`deno`,
"compile",
"--output",
"dist/bin/zsqk",
"--target",
"x86_64-unknown-linux-gnu",
"--allow-all",
"/User/zsqk/web/main.ts",
],
});
await r.status();
r.close();
Deno.writeTextFileSync(
`/User/zsqk/web/dist/bootstrap`,
"#!/bin/bash ./bin/zsqk --allow-all"
);

When the above command is executed successfully, we have completed the whole process of deploying the Deno Web application to Aliyun Serverless with one click. Next, we can use the access domain name configured above to check whether our UA Web works normally.

So far, we have finished the whole work. If we need to apply code changes every time, we just need to re-execute the build and deployment commands.

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us