Treehouse
NodeJS utilities and handy helpers extending ExpressJS functionalities
![Greenkeeper badge](https://badges.greenkeeper.io/icapps/tree-house.svg)
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: {},
helmet: {},
})
setBodyParser(app, route, options)
Set a body parser using the body-parser
module
const app = express();
treehouse.setBodyParser(app, '*', {
json: {},
raw: {},
text: {},
urlEncoded: {},
})
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();
const globalRateLimiter = treehouse.getRateLimiter({
max: 100,
delayMs: 0
windowMs: 60 * 60 * 1000,
message:
"Too many accounts created from this IP, please try again after an hour"
});
app.use('/login', globalRateLimiter, ...);
treehouse.getRateLimiter({
redis: {
client: existingClient,
},
});
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) {
}
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,
post: postFn,
https: {
port: 3001,
privateKey: 'assets/ssl.key',
certificate: 'assets/ssl.cert',
},
keepAliveTimeout: 60000,
headersTimeout: 60000,
})
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();
await treehouse.setSwagger(app, '/documentation', 'documentation/swagger.yml', {
host: 'localhost:3000',
schemes: ['http'],
};
Folder implementation with valid structure
Structure
.
├── validFolderName
| ├── index.yml
| └── routes
| ├── route1.yml
| └── randomName.yml
| ├── ...
Example code
const app = express();
treehouse.setSwagger(app, '/documentation', 'documentation/validFolderName', {
host: 'localhost:3000',
schemes: ['http'],
concatenate : true,
};
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: Joi.object({
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