npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

aws-serverless-restify

v0.0.6

Published

A restify like interface for aws lambda with api gateway event source

Downloads

15

Readme

lambda-restify

Build Status

A restify/expressjs like interface for aws lambda with api gateway event.

Installation

npm install --save lambda-restify

What is it about

If you are writing aws lambda function to develop rest apis using aws api gateway, this package will help you with request/response/routing/middlewares/versioned apis type features generally found in packages like restify or express.

Instead of using http module for opening a server and listening for incoming requests, this package relies on lambda event and callback.

When you make an http request against aws apigateway it triggers aws lambda with an event containing all the information about the incoming request (like method, url, querystring, headers, and body). lambda-restify relies on that information to create request object.

When your route handler sends response back (including headers, content), lambda-restify triggers lambda callback.

Supported features

  • Full support for restify request/response api
  • Pre routing hooks
  • Middlewares
  • Routing
  • Versioned apis

Dependency

It requires node >= 6.10.0. Make sure you choose "6.10.2" or above while creating lambda function. At the time of writing lambda supports v4.3.2 and 6.10.2.

Getting started

Install the package

npm install --save lambda-restify

Create server

See list of supported options here.

const Server = require('lambda-restify').default;
const server = new Server(options);

Or, if you are using imports

import Server from 'lambda-restify';
const server = new Server(options);

Attach your routes and middlewares

See restify documentation for documentation on server.pre, server.use, server.get (and other http verbs). Since lambda-restify uses restify like interface all that docs apply here as well.

server.pre(function(req, res, next) {
    // this handler is run for all routes, even 404
    
    // do something here
    next()
})

server.use(function(req, res, next) {
    // this handler is run for after routing is done 
    // and successful match is found (not on 404)

    // do something here
    next()
})

server.post('/user/:id', function(req, res) {
    
    // headers available 
    const apiKey = req.header('apikey');
    
    // route param
    const userId = req.params.id;
    
    // query string
    const queryValue = req.query('queryKey')

    // body
    const name = req.body.name

    // send response with res.json or res.send
    res.json({
        status: 1
    })
})

// define other route handlers

Attach lambda handler

exports.yourlambdaHandler = function(event, context, callback) {
    server.handleLambdaEvent(event, context, callback)
}

Documentation

Note Most likely, you will need to set:

context.callbackWaitsForEmptyEventLoop = false 

before calling server.handleLamdaEvent in your lamda handler. See http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html for details.

See restify documentation. Following items work just as they did in restify:

  • Request:
    • headers
    • url
    • httpVersion
    • method
    • params
    • body
    • rawBody
    • header(name: string, defaultValue: string)
    • accepts(type: string | string[])
    • acceptsEncoding(type: string | string[])
    • getContentLength()
    • contentLength() [alias of getContentLength]
    • getContentType()
    • contentType() [alias of getContentType]
    • time()
    • date()
    • getQuery()
    • query() [alias of getQuery()]
    • getUrl()
    • href()
    • id(reqId?: string)
    • getId()
    • getPath()
    • path() [alias of getPath]
    • is(type: string)
    • isSecure()
    • isChunked()
    • toString()
    • userAgent()
    • version()
    • matchedVersion()
    • trailer(name: string, value?: string) [no trailers support. it just pass back the default value]
    • isKeepAlive()
    • isUpload()
  • Response
    • finished
    • headersSent
    • sendDate
    • statusCode
    • statusMessage
    • serverName
    • cache(type?: any, options?: any)
    • noCache()
    • header(name: string, value?: any)
    • setHeader(name: string, value: any)
    • getHeaders()
    • headers() [alias of getHeaders]
    • send(code?: number, body?: string | json, headers?: json)
    • sendRaw(code?: number, body?: string | json, headers?: json)
    • removeHeader(name: string)
    • writeHead(code?, message?, headers?)
    • write(chunk: string | Buffer, encoding?: string, callback?: any)
    • end(data?: string | Buffer, encoding?: string, callback?)
    • get(name: string)
    • json(code?, body?, headers?)
    • link(l, rel)
    • charSet(type: string)
    • redirect(...)
    • status(code: number)
    • set(name: string | object, val?: string)
    • getHeaderNames()
    • hasHeader(name: string)
  • Server
    • pre(handlers)
    • use(handlers)
    • get(path?, options?, handlers)
    • del(path?, options?, handlers)
    • head(path?, options?, handlers)
    • opts(path?, options?, handlers)
    • post(path?, options?, handlers)
    • put(path?, options?, handlers)
    • patch(path?, options?, handlers)
    • param(name, fn)
    • versionedUse(versions: string | string[], fn)

Server.handleLambdaEvent(lambdaEvent, context, lambdaCallback) Plug this into lambda handler to route all incoming lambda events.