0.0.3 • Published 1 year ago

express-handle v0.0.3

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

Express handle

This is a Node.js package that automatically loads routes in an Express app from files in a specified folder. It also allows for customization of middleware and parameters for the loaded routes.

Installation

npm install express-handle

Usage

const express = require('express');
const { handler } = require('express-handle');

const app = express();

const options = {
folder: './routes',
middlewares: [ /* array of middleware functions */ ],
params: { "param1": "value1", "param2": "value2" },
notfound: (req, res) => { /* custom 404 handler function */ },
tree: true // display route table
};

handler(app, options);

app.listen(3000, () => console.log('App is listening on port 3000'));

Route File Example

Here is an example of a route file that can be loaded:

module.exports = {
name: '/hello',
method: 'GET',
description: 'Returns a hello message',
disabled: false,
run: (req, res) => {
   res.send('Hello World!');
  }
}

Explanation:

Suppose we have a route file routes/hello.js as shown above. When we run the Express app with the auto loader and navigate to http://localhost:3000/hello, we will see the message "Hello World!" displayed in the browser.

Additionally, if the tree option is set to true, a table showing all loaded routes will be displayed in the console when the app is started.

Commands
┌────────┬──────────────────────┬────────┬─────────┐
│ Name │ Description │ Method │ Disabled│
├────────┼──────────────────────┼────────┼─────────┤
│ /hello │ Returns a hello message │ GET │ false │
└────────┴──────────────────────┴────────┴─────────┘

Note: To use the params option to pass parameters to the route file's run function, make sure to pass a stringified JSON object as the value of the option.

Contributing

Contributions are welcome! Feel free to open an issue or submit a pull request.

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

Credits

Credits goes to abdlmutii, if you like this npm package please remember to check other work i've done on github and octo star abdlmutii/octoshell 😉. Email: abdlmutii@outlook.com Business Email: abdlmutii.buz@outlook.com