JSPM

  • Created
  • Published
  • Downloads 75
  • Score
    100M100P100Q57999F
  • License MIT

NodeJS utilities and handy helpers extending ExpressJS functionalities

Package Exports

  • tree-house

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (tree-house) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Treehouse

NodeJS utilities and handy helpers extending ExpressJS functionalities

npm version Dependencies Build Status Coverage Status

Installation

Install via npm

npm install tree-house

or via yarn

yarn add tree-house

Usage

const treehouse = require('tree-house')
import * as treehouse from 'tree-house'

Security

setLocalHeaders(app, route)

Only for development purposes!

Set headers to allow all options calls responding with a 204. This will prevent web applications from receiving an unauthorised response when trying to send a request from localhost.

const app = express();

treehouse.setLocalHeaders(app, '*')

setBasicSecurity(app, route, options)

Set some basic Express security using cors and helmet.

const app = express();

treehouse.setBasicSecurity(app, '*', {
  cors: {},   // cors options
  helmet: {}, // helmet options
})

setBodyParser(app, route, options)

Set a body parser using the body-parser module

const app = express();

treehouse.setBodyParser(app, '*', {
  json: {},   // json options
  raw: {}, // raw options
  text: {}, // text options
  urlEncoded: {}, // urlEncoded options
})

setRateLimiter(app, route, options)

Set a rate limiter to prevent brute force attacks. At the moment there is support for a built in-memorystore or Redis. Both use the express-brute module.

const app = express();

// In memory store (development purposes)
treehouse.setRateLimiter(app, '*', {
  freeRetries: 10,
})

// Using existing Redis client
treehouse.setRateLimiter(app, '*', {
  redis: {
    client: existingClient, // All Redis options or 'client' to use an existing client (see redis-express-brute)
  },
})

Responder

handleAsyncFn((req, res, next(optional)) => { ... })

Express middleware that wraps and executes a given function with try/catch to avoid unhandled promises within Express.

const app = express();

function getAllUsers(req, res) {
  //  res.send(users) -> return users...
  // or
  // if an unhandled error occurs this will be passed onto the Express error handler instead of raising an UnhandledPromiseRejectionError
}

app.use('/users', treehouse.handleAsyncFn(getAllUsers));

Server

startServer(app, options)

Start an http or https server using an express instance

const app = express();

treehouse.startServer(app, {
  port: 3000,
  title: 'My app'   // optional
  https: {          // optional
    port: 3001,
    privateKey: 'assets/ssl.key',
    certificate: 'assets/ssl.cert',
  }
})

Swagger

setSwagger(app, route, filePath, options)

Serve Swagger UI via the a provided Swagger yaml file.

const app = express();

treehouse.setSwagger(app, '/documentation', 'documentation/swagger.yml', {
  host: 'localhost:3000',
  schemes: ['http'],
});

Tests

  • You can run yarn test to run all tests
  • You can run yarn test:coverage to run all tests with coverage report

Bugs

When you find issues, please report them:

Be sure to include all of the output from the npm command that didn't work as expected. The npm-debug.log file is also helpful to provide.

Authors

See 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