New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

hapi-lambda

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hapi-lambda

Support Hapi 17+ applications on Amazon Lambda

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

hapi-lambda

npm version

This module will allow you to host your Hapijs (17+) application on Amazon Lambda with node 8.10+. If you are using API Gateway, you should set the Gateway to "proxy" mode.

CAUTION: There are significant breaking changes between this version and the pre-1.0 versions of this module.

Usage

// api.js

const Hapi = require('@hapi/hapi');

module.exports = {
  init: async () => {
    const server = new Hapi.server({
      port: process.env.port || 3000,
      routes: { cors: true }
    });

    const plugins = []; // your plugins here
    await server.register(plugins);

    // return the server for Lambda support
    return server;
  },
};

Your index.js file that you expose to Lambda should look like the following:

// index.js

const api = require('./api');
const { transformRequest, transformResponse } = require('hapi-lambda');

// cache the server for better peformance
let server;

exports.handler = async event => {
  if (!server) {
    server = await api.init();
  }

  const request = transformRequest(event);

  // handle cors here if needed
  request.headers['Access-Control-Allow-Origin'] = '*';
  request.headers['Access-Control-Allow-Credentials'] = true;

  const response = await server.inject(request);

  return transformResponse(response);
};

Deployment

Deployment is a much larger topic and not covered by this module, however I highly recommend deploying your Lambda application with Serverless

Usage with Serverless

Here is an example serverless configuration.

service: hapi-lambda-demo
provider:
  name: aws
  runtime: nodejs8.10

stage: dev
region: us-east-1

functions:
  api:
    handler: index.handler
    events:
      - http:
          path: "{proxy+}"
          method: any
          cors: true

plugins:
  - serverless-offline

Demo

A working repository and example is provided at https://www.carbonatethis.com/hosting-a-serverless-hapi-17-api-with-aws-lambda/

Keywords

web

FAQs

Package last updated on 30 Apr 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts