tree-house
NodeJS utilities and handy helpers extending ExpressJS functionalities
Last updated 5 months ago by knor-el-snor .
ISC · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install tree-house 
SYNC missed versions from official npm registry.

Treehouse

NodeJS utilities and handy helpers extending ExpressJS functionalities

npm version Dependencies Build Status Coverage Status Greenkeeper badge

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

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
})

getRateLimiter(options)

Get a rate limiter instance to prevent brute force attacks. This can be used as a middleware in Express. At the moment there is support for a built in-memorystore or Redis. Both use the express-rate-limit module.

const app = express();

// In memory store (development purposes)
const globalRateLimiter = treehouse.getRateLimiter({
  max: 100, // limit each IP to 100 requests per windowMs
  delayMs: 0 // disable delaying - full speed until the max limit is reached
  windowMs: 60 * 60 * 1000, // 1 hour window
  message:
    "Too many accounts created from this IP, please try again after an hour"
});

app.use('/login', globalRateLimiter, ...);

// Using existing Redis client
treehouse.getRateLimiter({
  redis: {
    client: existingClient, // All Redis options or 'client' to use an existing client (see rate-limit-redis)
  },
});

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',
  pre: preFn,       // function to execute before starting server (optional)
  post: postFn,     // function to execute after starting server (optional) - will contain the http server as first argument
  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 OR folder with valid structure and yaml files.

YAML file implementation

const app = express();

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

Folder implementation with valid structure

Structure

.
├── validFolderName
|   ├── index.yml # contains basic info + definition models
|   └── routes
|          ├── route1.yml
|          └── randomName.yml
|          ├── ... # more yml files

Example code

const app = express();

treehouse.setSwagger(app, '/documentation', 'documentation/validFolderName', {
  host: 'localhost:3000',
  schemes: ['http'],
  concatenate : true, // The property to enable folder functionality
};

Validator

validateSchema(schema, options)

Express middleware to validate a Joi schema using the express-validation module. This will throw an error as an instance of ExpressValidationError if the Joi validation fails.

const schema =   {
  body: {
    name: Joi.string().required(),
  }
};

app.post('/my-endpoint', treehouse.validateSchema(schema), ...);

Tests

  • You can run npm run test to run all tests
  • You can run npm run 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 ISC License - see the LICENSE.md file for details

Current Tags

  • 4.0.0                                ...           latest (5 months ago)

49 Versions

  • 4.0.0                                ...           5 months ago
  • 3.4.5                                ...           6 months ago
  • 3.4.4                                ...           6 months ago
  • 3.4.3                                ...           7 months ago
  • 3.4.2                                ...           9 months ago
  • 3.4.1                                ...           9 months ago
  • 3.4.0                                ...           10 months ago
  • 3.3.2                                ...           a year ago
  • 3.3.1                                ...           a year ago
  • 3.3.0                                ...           2 years ago
  • 3.2.0                                ...           2 years ago
  • 3.1.0                                ...           2 years ago
  • 3.0.5                                ...           2 years ago
  • 3.0.4                                ...           2 years ago
  • 3.0.3                                ...           2 years ago
  • 3.0.2                                ...           2 years ago
  • 3.0.1                                ...           2 years ago
  • 3.0.0                                ...           2 years ago
  • 2.2.3                                ...           2 years ago
  • 2.2.2                                ...           2 years ago
  • 2.2.1                                ...           2 years ago
  • 2.2.0                                ...           2 years ago
  • 2.1.5                                ...           2 years ago
  • 2.1.4                                ...           2 years ago
  • 2.1.3                                ...           2 years ago
  • 2.1.2                                ...           2 years ago
  • 2.1.1                                ...           2 years ago
  • 2.1.0                                ...           2 years ago
  • 2.0.0-alpha.8                                ...           2 years ago
  • 2.0.0-alpha.7                                ...           2 years ago
  • 2.0.0-alpha.6                                ...           2 years ago
  • 2.0.0-alpha.5                                ...           2 years ago
  • 2.0.0-alpha.4                                ...           2 years ago
  • 2.0.0-alpha.3                                ...           2 years ago
  • 2.0.0-alpha.2                                ...           2 years ago
  • 2.0.0-alpha.0                                ...           2 years ago
  • 1.0.2                                ...           2 years ago
  • 1.0.1                                ...           2 years ago
  • 1.0.0                                ...           2 years ago
  • 0.1.5                                ...           2 years ago
  • 0.1.3                                ...           2 years ago
  • 0.1.2                                ...           2 years ago
  • 0.1.1                                ...           3 years ago
  • 0.1.0                                ...           3 years ago
  • 0.0.5                                ...           3 years ago
  • 0.0.4                                ...           3 years ago
  • 0.0.3                                ...           3 years ago
  • 0.0.2                                ...           3 years ago
  • 0.0.1                                ...           3 years ago
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 50
Dependencies (10)
Dev Dependencies (20)

Copyright 2014 - 2016 © taobao.org |