dir-routes

No configuration file.
Intuitive.
Maintainable.
Simple.

Use your folder structure as routes, it uses the path as route and filename as HTTP method, also works with route “parameters”. Consider the following project folder:

-| node_modules
-| routes
 | -| new
 |  | -| @id
 |  |  | -| post.js
 | -| get.js
-| index.js

Inside the routes folder we have two files: /new/@id/post.js and /get.js; now lets take a look at each one of them.

/get.js

module.exports = function(req,res){
	res.send('a html form');
}

Full path: project_dir/routes/get.js
Will be mapped to /

/new/@id/post.js

module.exports = function(req,res){
	req.params.id; //do something
	res.send('a html response');
}

Full path: project_dir/routes/new/@id/post.js
Will be mapped to /new/:id

index.js

Finally lets see the application entry point.

var express = require('express'),
http = require('http'),
app = express(),
router = require('dir-routes');

app.use(router); // <- dir-routes exports a Express Router object, just use it

app.use(function(req, res) {
	res.status(404).end('404: Page not Found');
});

var httpServer = http.createServer(app);

httpServer.listen(8080, function() {
	console.log('Listening on port %d', httpServer.address().port);
});

Example

A simple registration and login application, built only with express, dir-routes and 3 routes.
Download!

After downloaded extract the folder and execute with:
node example

Get it!

npm install dir-routes

License

MIT

Sponsor

Logo Dokoro