简体   繁体   English

要求文件夹作为模块在另一个文件夹作为模块内

[英]require folder as module inside another folder as module

At server.js I've got the following: server.js中,我有以下内容:

app.use( require( './server/rest-api/v1/products' ) );
app.use( require( './server/rest-api/v1/product-categories' ) );
app.use( require( './server/rest-api/v1/measuring-units' ) );
app.use( require( './server/rest-api/v1/inventory' ) );
app.use( require( './server/rest-api/v1/suppliers' ) );
...

What I want is to make of /server/rest-api/v1 a module, that means that it needs an index.js , but what would it contain so that I'll only have to do the following at server.js : 我想要的是将/server/rest-api/v1做成一个模块,这意味着它需要一个index.js ,但是它将包含什么内容,因此我只需要在server.js上执行以下操作:

app.use( require( './server/rest-api/v1');

Here's one of the folders as modules I have if is necessary to understand what I ask: 如果需要理解我的要求,这是我作为模块使用的文件夹之一:

/server/rest-api/v1/products/index.js /server/rest-api/v1/products/index.js

module.exports = (function () {

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

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

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

    router.route( '/api/v1/purchases/products/detail/:id' )
        .get( detail_product.get );

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

    return router;

})();

You want /server/rest-api/v1/index.js to expose a unique Router that uses sub-routers, each corresponding to one of your folders ( products , product_categories , etc.) 你想/server/rest-api/v1/index.js暴露一个独特的Router使用子路由器,每个对应文件夹的一个( 产品 ,product_categories等)

/server/rest-api/v1/index.js /server/rest-api/v1/index.js

var express = require('express'),
    router = express.Router(),
    product_router = require('./products'),
    product_categories_router = require('./product_categories');

router.use(product_router);
router.use(product_categories_router);
// etc.

module.exports = router;

On a side note , if you are dealing with many routes this way, it may be handier for you to define your API entry point once ( '/api/v1' ), when mounting the router. 附带说明一下 ,如果您以这种方式处理许多路由,则在安装路由器时一次定义API入口点( '/api/v1' )可能比较麻烦。 This way your "business" routers don't need to know the entry path themselves (and it should not matter to them), which is convenient if you ever need to change that path one day. 这样,您的“企业”路由器就不需要自己知道入口路径(这对他们来说也没有关系),如果您有一天需要更改该路径,这将很方便。

Then again this is up to you and how you want to design your server :) 然后再次取决于您以及您如何设计服务器:)

Example : 范例:

server.js server.js

app.use('/api/v1', require('./server/rest-api/v1'));

/server/rest-api/v1/index.js /server/rest-api/v1/index.js

var express = require('express'),
    router = express.Router(),
    product_router = require('./products');

router.use('/purchases/products', product_router);
module.exports = router;

/server/rest-api/v1/products/index.js /server/rest-api/v1/products/index.js

var express = require('express'),
    router = express.Router(),
    create_product = require('./create-product.controller');

router.route('/new').post(create_product.post);
module.exports = router;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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