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

aera-tools

v1.0.7

Published

Handy HTTP tools to use with Aera HTTP library.

Downloads

13

Readme

Aera Tools

Handy HTTP tools to use with Aera HTTP library.


Build Status Coverage Status Dependencies devDependencies Status npm

List of Tools

  • Http Error - create an Error object with HTTP specific proerties.
  • Body Parser - turn request bodies into promises, resolving into JavaScript Objects.
  • Compose - if you prefer middleware-based approach, it can be done with this module.
  • Middleware - create real express-like middleware.
  • Static - Serve static files even easier!
  • Cors - configure Cross Origin Resource Sharing!

Http Error

This constructor creates an easy-to-use Error object, that is an instance of JavaScript Error, but also has a status property. Throw this instead of regular errors and Aera will automatically give better error responses.

Example usage

const { HttpError } = require('aera-tools')

server.get('/auth', () => new HttpError('You are not logged in.', 401)) // Response will be `You are not logged in.` with status 401.

Body Parser

Request bodies are generally kind of hard to parse, especially of no proper content type has been set on the request (which this module does not help with at the moment, by the way). This tool takes in the request object and returns a promise, that resolves into the parsed body object, or rejects into an empty object for your convenience.

Example usage

const { parseBody } = require('aera-tools')

server.post('/', (req, res) => parseBody(req)) // this will return the body parsed into an object

You can also do some stuff with the body, once it's been resolved.

server.post('/', (req, res) => bodyParser(req).then(createResourceInDb))

Compose

Aera does not encourage middleware-based approach to writing your HTTP applications, however if you do want to provide multiple functions in sequence that you want executed, you can use this tool.

Example usage

const { compose } = require('aera-tools')

server.get('/', compose(req, res, function1, function2))

Each of these functions will have full access to request and response. E.g.:

server.get('/', compose(
  (req, res) => res.setHeader('X-Custom-Header', 'value'),
  (req, res) => console.log(res.getHeader('x-custom-header') === 'value'), // logs true
  (req, res) => 'Hello, my app!'
))

As you can see, there is no calling next in this tool. That will be the middleware tool.

Middleware

If you want to use real express-style middleware with Aera, you can do so, with this tool. Do note that this will somewhat decrease the performance of Aera, so if you've made your application logic truly decoupled from the feedback logic, this tool should not be needed.

Example usage

const { middleware } = require('aera-tools')

server.get('/', middleware(
  (req, res, next) => {
    let t1 = Date.now()
    next()
    console.log(`Request took: ${Date.now() - t1} milliseconds.`)
  }, (req, res, next) => {
    return 'Hello, World!'
  }
))

Static

Note: this will only work, from version 1.1.0 of Aera, due to how arguments are handled in the newer version.

Serves all files from the directory specified.

Example usage

const { static } = require('aera-tools')

server.get(static('/public', './my/file/folder'))

The above example will serve requests coming to /public with the contents of /my/file/folder.

Cors

Configure a route to use Cross Origin Resouce Sharing policies properly. Note that this will only work for the specific route you called it in, but there is nothing stopping you from adding it to all the routes you want to allow CORS on.

Make sure you also configure the OPTIONS method too.

Example usage

const { cors } =require('aera-tools')

server.get('/', (req, res) => {
  cors(req, res)
  return 'Hello, World!'
})

server.options('/', cors)

Cors also accepts options. Here is a list of them:

  • origin defaults to true
  • expose defaults to ''
  • maxAge defaults to false
  • credentials defaults to false
  • methods defaults to '*'
  • headers defaults to *, falls back to Access-Control-Request-Headers.