Giter Club home page Giter Club logo

node-apex's Introduction

Apex Node.js

Node.js module that makes AWS Lambda's user experience a little nicer.

export default λ(e => 'Hello World')

Installation

$ npm install --save apex.js

Features

  • return promises
  • return results
  • actually report uncaught errors (Lambda does not)

Example

The following example fetches some urls and reports the response status of each. The context and callback are also passed, but are not shown here.

import axios from 'axios'
import λ from 'apex.js'
import 'babel-polyfill'

export default λ(e => {
  console.log('fetching %d urls', e.urls.length)
  return Promise.all(e.urls.map(async (url) => {
    console.log('fetching %s', url)
    return {
      status: (await axios.get(url)).status,
      url
    }
  }))
})

Without this module it looks something like the following, as Lambda does not try/catch, and the Context provided has awkward method names that are not idiomatic, or you must use the callback.

import axios from 'axios'
import 'babel-polyfill'

// Vanilla Lambda function.
export default async (e, ctx, cb) => {
  console.log('fetching %d urls', e.urls.length)

  try {
    const res = await Promise.all(e.urls.map(async (url) => {
      console.log('fetching %s', url)
      return {
        status: (await axios.get(url)).status,
        url
      }
    }))

    // or ctx.succeed(res);
    cb(null, res);
  } catch (err) {
    // or ctx.fail(err);
    cb(err);
  }
}

Contributors

Badges


tjholowaychuk.com  ·  GitHub @tj  ·  Twitter @tjholowaychuk

node-apex's People

Contributors

tj avatar ajcrites avatar

Watchers

liangyali avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.