NodeJS utilities and handy helpers extending ExpressJS functionalities
Install via npm
npm install tree-houseor via yarn
yarn add tree-houseconsttreehouse=require('tree-house')import*astreehousefrom'tree-house'Set some basic Express security using cors and helmet.
constapp=express();treehouse.setBasicSecurity(app,'*',{cors: {},// cors optionshelmet: {},// helmet options})Set a body parser using the body-parser module
constapp=express();treehouse.setBodyParser(app,'*',{json: {},// json optionsraw: {},// raw optionstext: {},// text optionsurlEncoded: {},// urlEncoded 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.
constapp=express();// In memory store (development purposes)constglobalRateLimiter=treehouse.getRateLimiter({max: 100,// limit each IP to 100 requests per windowMsdelayMs: 0// disable delaying - full speed until the max limit is reachedwindowMs: 60*60*1000,// 1 hour windowmessage:
"Too many accounts created from this IP, please try again after an hour"});app.use('/login',globalRateLimiter, ...);// Using existing Redis clienttreehouse.getRateLimiter({redis: {client: existingClient,// All Redis options or 'client' to use an existing client (see rate-limit-redis)},});Express middleware that wraps and executes a given function with try/catch to avoid unhandled promises within Express.
constapp=express();functiongetAllUsers(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));Start an http or https server using an express instance
constapp=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 argumenthttps: {// optionalport: 3001,privateKey: 'assets/ssl.key',certificate: 'assets/ssl.cert',},keepAliveTimeout: 60000,// optionalheadersTimeout: 60000,// optional})Serve Swagger UI via the a provided Swagger yaml file OR folder with valid structure and yaml files.
constapp=express();awaittreehouse.setSwagger(app,'/documentation','documentation/swagger.yml',{host: 'localhost:3000',schemes: ['http'],};Structure
.
├── validFolderName
| ├── index.yml # contains basic info + definition models| └── routes
| ├── route1.yml
| └── randomName.yml
| ├── ... # more yml filesExample code
constapp=express();treehouse.setSwagger(app,'/documentation','documentation/validFolderName',{host: 'localhost:3000',schemes: ['http'],concatenate : true,// The property to enable folder functionality};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.
constschema={body: Joi.object({name: Joi.string().required(),})};app.post('/my-endpoint',treehouse.validateSchema(schema), ...);- You can run
npm run testto run all tests - You can run
npm run test:coverageto run all tests with coverage report
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.
See the list of contributors who participated in this project.
This project is licensed under the ISC License - see the LICENSE.md file for details