Folder-router is a tiny express router to get started with a new NodeJS project. You can put all your route files in a single directory, folder-router will be able to open and inject all your endpoint in your express instance. You can also put your file in sub directory and access it through sub-url.
You need NPM to install folder-router in your NodeJS project.
$ npm install --save folder-router
You can now use it in your main server file.
The first parameters must be your express instance, the second one must be the location of your routes directory. I highly recommand to use absolute location.
var app = require('express')();
var router = require('folder-router');
router(app, __dirname + '/routes');
app.listen(3000);
In your routes directory, you can create as much as file as you want, everything will be read and add to express by folder-router. A route file MUST export a express.Router() instance as below :
routes/book.js
var Router = require('express').Router();
Router.get('/book', function(req, res) {
res.end('All Books !');
});
Router.get('/book/:id', function(req, res) {
res.end('Books with id = ', req.params.id);
});
Router.post('/book', function(req, res) {
res.end('Adding a new book')
});
module.exports = Router;
Here is the list of all end points :
Also, you can create a directory "book" and write your routes like so :
routes/book/book.js
var Router = require('express').Router();
Router.get('/', function(req, res) {
res.end('All Books !');
});
Router.get('/:id', function(req, res) {
res.end('Books with id = ', req.params.id);
});
Router.post('/', function(req, res) {
res.end('Adding a new book')
});
module.exports = Router;
The name of the file has no impact. The end point remains the same because it takes the name of the sub directory
One other solution for that, you can exports the base url with the root variable
routes/book.js
var Router = require('express').Router();
Router.get('/', function(req, res) {
res.end('All Books !');
});
Router.get('/:id', function(req, res) {
res.end('Books with id = ', req.params.id);
});
Router.post('/', function(req, res) {
res.end('Adding a new book')
});
module.exports = Router;
module.exports.root = '/book'; // here
$ npm test
or
$ mocha ./test
All ways to contribute to folder-router are highly appreciated:
- improving the readme
- writing features
- writing tests
- writing a tutorial
- troubleshooting reported issues
Thank you very much !