简体   繁体   中英

express-resource custom mapping

I can't find any decent documentation on this that contains any depth when requiring with app.resource.

The only information that I can find is creating the variable within the current file. Here

The current code that I have is below:

var favs = app.resource('favs', require('./modules/favs'));
favs.map('get', '/user', favs.buses);

However it comes back saying that it is undefined?

In the module favs I have.

exports.buses = function (req, res) {
    res.render('favs/buses', {
        title: 'Bus Stops'
    });
});

You have a syntax error in favs.js : The last right bracket ) is redundant.

Apart from that, you seem to conflate the actual module with the resource object you get back from app.resource . You want to pass a reference to the request handler you want to invoke when a visitor hits the path (in your case, /favs/user ). So what you want is something like:

var favs = require('./modules/favs'),
    favsResource = app.resource('favs', favs);

favsResource.map('get', '/user', favs.buses);

If you feel a little lost dealing with express-resource , I encourage you to begin with plain express, and only start using express-resource when you are more familiar with how express works. TJ's helper modules have a tendency to be lacking in documentation, and you should use them only if you feel comfortable reading the code, IMO.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM