Step 1: Project Setup
I will use Serverless framework for this tutorial. You can also use AWS
SAM
npm install -g serverless
In any folder run serverless
as below:
Step 2: API Implementation
Edit handler.js file as below. See the blog post for the details of the algorithm.Step 3: Create database on Upstash
If you do not have one, create a database following this guide. Copy the Redis URL by clickingRedis Connect
button inside database page. Copy the URL for ioredis as we use ioredis in our
application. Create .env file and paste your Redis URL:
This example uses ioredis, you can copy the connection string from the
Node tab in the console.
Step 4: Initialize Database
We will initialize the database with country names. Copy and run initdb.js script from here. We simply put the country names and all their prefixes to the sorted set.Step 5: Deploy Your Function
Editserverless.yml
as below and replace your Redis URL:
Test
button on the top right. Use { "queryStringParameters": {"term":"ar"}}
as your
event data.