1.2.2 • Published 8 years ago
express-autoload-routes v1.2.2
express-autoload-routes
A tiny (1kb) asynchronous auto-load routes with support for manual routes setup
Installation
$ npm i -S express-autoload-routes
Usage
Loading routes
/* file: /app.js */
const express = require('express');
const autoload = require('express-autoload-routes');
const app = express();
const pathBase = './routes/';
/* Defining manual routes (optional) */
const manualRoutes = {
'index.js': { // Filename in pactBase directory
filename: 'index.js', // Full name of file without directory name
url: '/', // Route URL
dir: null, // If file is in another directory
},
'ignore.js': { // File will be ignored
// Left blank to ignore file
}
};
autoload(pathBase, manualRoutes)
.then(route => app.use(route))
.catch((err) => {
// Do stuff here
});
Defining routes
/* file: /routes/index.js */
const express = require('express');
const router = express.Router();
/* GET API description. */
router.get('/', (req, res) => {
const { name, version } = pack;
res.send({ name, version });
});
module.exports = router;
How it works
Adds routes asynchronously so that the file name will be the address of the route.
Example:
Users.js => url: '/users'