TL;DR: Ready to migrate your KOA application to AWS, GCP, or Azure? Simplify the process by automating resource provisioning which is ready to scale up or down to zero as required by usage.
Let's take a look at an example of a KOA service that creates secure download URLs for images stored in a bucket like S3 or Blob Storage:
import Koa from "koa";
import Router from "koa-router";
import { http, bucket } from "@nitric/sdk";
export const imgBucket = bucket("images").for("reading", "writing");
const app = new Koa();
const router = new Router();
router.get("/download/:id", async (ctx) => {
try {
const { id } = ctx.params;
const img = imgBucket.file(`images/${id}/photo.png`);
ctx.body = {
url: await img.getDownloadUrl(),
};
} catch (err) {
ctx.throw(500, "Error getting file URL");
}
});
app.use(router.routes());
http(app, () => {
console.log(`Application started`);
});
This code sets up an HTTP server that handles requests for downloading images. It listens for a GET request to "/download/:id" and retrieves the corresponding image file from the "images" bucket. It then generates a download URL for the image and sends it back as the response.
To automate the deployment into the cloud we'll introduce the Nitric SDK. The only difference between your existing KOA app and this version is how the server is started. Instead of using the traditional approach, such as app.listen(port)
, we use the http import from the Nitric SDK.
http(app, () => {
console.log(`Application started`);
});
This modification allows Nitric to wire up your API Gateway in AWS, GCP, or Azure automatically. Additionally, the Nitric SDK provides access to cloud storage functionality, allowing you to interact with resources like buckets seamlessly.
To begin your deployment install the Nitric CLI, which containerizes and deploys your solution using traditional IaC tools like Pulumi and Terraform to create the necessary infrastructure.
Top comments (1)
Also have a code example written with the express framework to the same goal - for anyone who is interested!