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

lambda-api-router

v1.0.6

Published

Write serverless code like a traditional express application

Downloads

90

Readme

Lamb Chop

Lamb Chop provides an elegant and expressive syntax for using a single Lambda function as a proxy service for API Gateway routes. Write serverless code in a server oriented manner.

Getting Started

Before you can use this service make sure you setup your AWS API Gateway as a proxy service. You can create a single route which accepts any http request and path using the special /{path+}. Please see this AWS document for more information on setting up your lambda function as a proxy service.

Installing

You can install this package through NPM with

npm i --save lambda-api-router

Check out the following example for using the service in a live system:

const Api = require('lambda-api-router');
const app = new Api();

app.get('/foo/bar', (req, res) => {
    res.json({ success: true, test: 'Its working!', response: response.substring(0, 21) });
});

app.post('/foo/bar', (req, res) => {
    // ...
});

app.put('/users/find', (req, res) => {
    // ...
});

app.delete('/user/:id', (req, res) => {
    // ...
});

exports.handler = async (event, context) => app.listen(event, context);

Async Actions

You can also use asynchronous actions and promises within your route handlers:

const Api = require('lambda-api-router');
const request = require('request-promise-native');

const app = new Api();

app.get('/foo/bar', async (req, res) => {
    const response = await request('http://google.com');
    res.json({ success: true, response: response.substring(0, 21) });
});

exports.handler = (event, context) => app.listen(event, context);

Query & Request Params

You can easily access query and request parameters through req.query and req.params respectively.

const Api = require('lamb-chop');

const app = new Api();

app.get('/foo/bar', (req, res) => {
    res.json({ query: req.query, params: req.params });
});

exports.handler = (event, context) => app.listen(event, context);

Set Response Status & Headers

You can easily access query and request parameters through req.query and req.params respectively.

const Api = require('lambda-api-router');

const app = new Api();

app.get('/foo/bar', (req, res) => {
    const headers = { 
        Accept: 'application/json',
        'Content-Type': 'application/json'
    };
   
    res.headers(headers)
       .status(200)
       .json({ query: req.query, params: req.params });
});

exports.handler = (event, context) => app.listen(event, context);

Set Response Status & Headers

You can also add middleware to your routes. Middleware is simply a function which has access to the request and response objects which execute before the actual route handler is executed. Middleware is useful for things like checking authentication, adding headers, logging and other common web tasks.

You can register middleware using the use function like so:

const Api = require('lambda-api-router');
const jwt = require('jsonwebtoken');

const app = new Api();

// Logging Middleware
app.use((req, res) => {
    console.log(`[${req.httpMethod}] -- ${req.path} --`);
});

// Custom header Middleware
app.use((req, res) => {
    res.headers({
       'X-Custom-Header': 'my-custom-value', 
    });
});

// Authentication Middleware
app.use((req, res) => {
    const token = req.body.token;
    jwt.verify(token, 'shhhhh', function(err, decoded) {
        console.log(decoded.foo) // bar
        // if decoded.foo => proceed with request
        // else return res.error({ ... });
    });
});

app.get('/foo/bar', (req, res) => {
    const headers = { 
        Accept: 'application/json',
        'Content-Type': 'application/json'
    };
   
    res.headers(headers)
       .status(200)
       .json({ query: req.query, params: req.params });
});

exports.handler = (event, context) => app.listen(event, context);

Deployment

This is deployed through the deployment script called: ./scripts/publish.sh.

Built With

  • Node.JS - The web framework used
  • NPM - Dependency Management
  • Javascript - Programming language used
  • Lambda - Serverless web technology

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

  • Christian Bartram - Initial work - cbartram

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Acknowledgments

  • Express for creating a great framework