10/17/2019
Reading time: 2 mins

Serverless Framework

undefined


TASK:

Static website

http://cocktail.review.com.np/

Search bar does not work.

Add a data endpoint to serve drinks data to the autocomplete field.


Prerequisites

  • NodeJs
  • AWS account
  • AWS CLI

Install Serverless cli

# Install the serverless cli

npm install -g serverless

Login to Serverless

serverless login

Bootstrap a new service

serverless

Follow the setup instruction in your terminal. You will have to select application and name for the service.


Last bit

Use EU Central (Frankfurt) as region for your function

region: eu-central-1

Select trigger (API Gateway Endpoint)

events:
      - http:
          path: search
          method: get
          cors: true

Data for autocomplete

const drinks = [
  {
      name: "69 Special",
      slug: "69-Special",
      category: "Ordinary Drink",
  },
  {
      name : "Allies Cocktail",
      slug : "Allies-Cocktail",
      category : "Ordinary Drink",
  },
  ];

module.exports = {drinks};

Enable CORS

Add headers to your response

headers: {
      'Access-Control-Allow-Origin': '*',
      'Access-Control-Allow-Credentials': true,
    },

Add Response

body: JSON.stringify(
      {
        message: "success",
        results: {
          drinks: drinks
        }
      },
      null,
      2
    ),

Deploy

Deploy to dev env

sls deploy -s dev

Run Locally

sls invoke local -f hello
Previous
Is the moon moving with you, when you are moving?
Next
Machine Learning – On your Browser
© 2023 Anil Maharjan