使用router.route()和app。<post,get ...>之间有什么区别吗?

时间:2015-11-05 16:46:48

标签: node.js rest express

我正在为我的应用程序构建REST API(它是一个大型应用程序)所以我决定使它模块化,这是我到目前为止所做的处理路线:

/server/modules/purchases/products/index.js

module.exports = (function () {

    var express        = require( 'express' ),
        router         = express.Router(),
        create_product = require( './create-product.controller' ),
        list_product   = require( './list-product.controller' ),
        detail_product = require( './detail-product.controller' ),
        update_product = require( './update-product.controller' );

    router.route( '/purchases/products/new' )
        .post( create_product.post );

    router.route( '/purchases/products/list' )
        .get( list_product.get );

    router.route( '/purchases/products/detail' )
        .get( detail_product.get );

    router.route('/purchases/products/update')
        .put( update_product.put );

    return router;

})();

/server/modules/purchases/products/list-product.controller.js

module.exports = (function () {

    models = require( '../../database/models' );

    function get( request, response ) {
        //Send All Products
    }

    return {
        get: get
    }

})();

搜索时,我发现其他人为路由做的事情是:

...
var app = express();

app.post('/login',function(req,res){
  //Some code...
});
...

与我所做的有什么不同?如果它们具有可比性,那么哪种方式更好,我也想知道在没有得到的情况下设置帖子是否正确:

router.route( '/purchases/products/new' ).post( create_product.post );

0 个答案:

没有答案