简体   繁体   English

如何外包Node.js RESTful API调用?

[英]How to outsource Node.js RESTful API calls?

Currently I have the following Code and so much more Calls inside the File api.js, but now the file is so huge that it isn't easy to find anything anymore so I wanna outsource different routes in different files like api_admin.js, api_user.js, api_general.js, is it possible to outsource different calls to different files and include them? 目前,我在文件api.js中拥有以下代码和更多调用,但是现在文件太大,以至于再也找不到任何内容,因此我想将不同的路由外包给不同的文件,例如api_admin.js,api_user .js,api_general.js,是否可以将不同的调用外包给不同的文件并包含它们?

app.use( '/assets', express.static( __dirname + '/public/assets' ) );

// Serve index.html
app.get( '/', function( req, res ) {
    // ...
} );

// Serve Partials
app.get( '/views/:name', function( req, res ) {
    // ...
} );

app.get( '/views/user/:name', function( req, res ) {
    // ...
} );

app.get( '/views/admin/:name', function( req, res ) {
    // ...
} );

// Serve Data
var router = express.Router();
app.use( '/v1', router );
router.post( '/user/get/info', function( req, res ) {
    // ...
} );

router.post( '/admin/user/list', function( req, res ) {
    // ...
} );

router.post( '/admin/apps/list', function( req, res ) {
    // ...
} );

Just create a new module. 只需创建一个新模块即可。 Put a bunch of the route handlers in that new module. 将一堆路由处理程序放入该新模块中。 Then, load that new module form your api.js and pass it the app or router object in the module constructor. 然后,从api.js加载该新模块,并将其传递给模块构造函数中的approuter对象。

For example, here's how you break out some of the routes for admin: 例如,以下是您分解一些admin路线的方法:

api.js api.js

app.use( '/assets', express.static( __dirname + '/public/assets' ) );

// Serve index.html
app.get( '/', function( req, res ) {
    // ...
} );

// Serve Partials
app.get( '/views/:name', function( req, res ) {
    // ...
} );

app.get( '/views/user/:name', function( req, res ) {
    // ...
} );

app.get( '/views/admin/:name', function( req, res ) {
    // ...
} );

// Serve Data
var router = express.Router();
app.use( '/v1', router );
router.post( '/user/get/info', function( req, res ) {
    // ...
} );

// load and initialize admin routes
require('api_admin.js')(router);

api_admin.js api_admin.js

module.exports = function(router) {

    router.post( '/admin/user/list', function( req, res ) {
        // ...
    } );

    router.post( '/admin/apps/list', function( req, res ) {
        // ...
    } );
}

You can repeat this pattern for any other groups of routes, passing the loaded module either the app object or the router object in the module constructor. 您可以对任何其他路由组重复此模式,在模块构造函数中将加载的模块传递给app对象或router对象。 If you have common functions that several routing modules want to use, then put those in their own module, export them from that module and then require() them into any module that wants to use them. 如果您具有几个路由模块要使用的通用功能,则将其放在自己的模块中,从该模块中导出它们,然后将require()集成到要使用它们的任何模块中。

Try to use require and module.exports 尝试使用require和module.exports

var user = require('./file');

File content example: 文件内容示例:

var User = function(name, email) {
    this.name = name;
    this.email = email;
};
module.exports = User;

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

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