easy-route

a simple router for express.js

npm install easy-route
1 downloads in the last week
4 downloads in the last month

easy-route

a simple router for express.js. it is based on dir name, filename, method name, just be natural and humanity.

Old days

in some route file such as ./routes/site.js, you may code like this :

exports.github = function(req, res){
    res.render('coming', {title:'coming soon ...', selected:'github'})
}

then you need to exposed the 'github' method manually in app.js or some other file like this :

var site = require('./site')
app.get('/site/github', site.github)

once you add a method in your route file, you have to update app.js to expose it. meanwhile, you have to decide which http method is to be exposed (like GET and POST). once you wanna change it, you have to sync the two files manually. that is really boring,do you think so?

Now

in your app.js file, you could code like this :

require('easy-route').load(app, function(){
    http.createServer(app).listen(app.get('port'), function(){
        console.log("Express server listening on port " + app.get('port'));
    });
})

and it is over! just enjoy yourself, easy-route will help you finish the boring exposed jobs.

Exposed

for the file ./routes/site.js, the exposed url/api is :

/site/github

for the file ./routes/index.js, default settings has removed 'index'.however, you can change it simply.

/method_name

for the file ./routes/site/child/main.js, if the exports has a index method, the exposed api/url will be :

/site/child/main

for some other method as 'test', it will be :

/site/child/main/test

Http Method

generally, we use app.all to expose all the method, but you can simply change it like this :

exports.github = function(req, res){
    'post'
    res.render('coming', {title:'coming soon ...', selected:'github'})
}

now the github method is http post only, you can also set it to get and so on.

Caution

take a look when your script file's name is index.js or your file has a index method. for example, when you organize you routes folder, be careful on this condition :

./routes
    site.js (has a index method) => /site/
    index.js (has a site method) => /site

well, this may be problem and is a little weird. but i will not solve it just because i am strongly against the method named index. it is totally meaningless! do not make a index file or method, take it as the better practice.

Bugs

a know bug to fix. it happens when you use aop or some other utility like underscore's wrap function

exports.github = _.wrap(expoort.github, function(fn, req, res, next){
    //code here
})

now the toString method can not get the original function's directive such as get / post.

i am still trying to find a better way to solve this problem.

if you have any good idea, report it to me.

npm loves you