0

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?

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 ) {
    // ...
} );
  • Possible duplicate of [In Node.js, how do I "include" functions from my other files?](http://stackoverflow.com/questions/5797852/in-node-js-how-do-i-include-functions-from-my-other-files) – Dominik Jun 23 '16 at 13:40

2 Answers2

1

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.

For example, here's how you break out some of the routes for admin:

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

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. 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.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
0

Try to use require and module.exports

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

File content example:

var User = function(name, email) {
    this.name = name;
    this.email = email;
};
module.exports = User;
  • That's clear, I use it like that for config files. But how to use it with express routes? Can't use any express-specify stuff inside outsourced files. – Dailys Deed Jun 23 '16 at 14:14