app.js
/**
 * Loads all routes declared in routes/ into the app
 * @param {Application} app the application object to load routes into
 * @return {bluebird} a promise resolving to the app object
 */
Loads all routes declared in routes/ into the app
@param {Application} app the application object to load routes into
@return {bluebird} a promise resolving to the app object
Loads all routes declared in routes / into the app @param { Application } app the application object to load routes into @return { bluebird } a promise resolving to the app object
function loadRoutes(app) {
    return fs.readdirAsync(`${__dirname}/routes`).map((fname) => {
        return BBPromise.try(() => {
            if (!/\.js$/.test(fname)) {
                return undefined;
            }
            const route = require(`${__dirname}/routes/${fname}`);
            return route(app);
        }).then((route) => {
            if (route === undefined) {
                return undefined;
            }
            if (route.constructor !== Object || !route.path || !route.router
                || !(route.api_version || route.skip_domain)) {
                throw new TypeError(`routes/${fname} does not export the correct object!`);
            }
            if (route.path[0] !== '/') {
                route.path = `/${route.path}`;
            }
            if (route.path[route.path.length - 1] !== '/') {
                route.path = `${route.path}/`;
            }
            if (!route.skip_domain) {
                route.path = `/:domain/v${route.api_version}${route.path}`;
            }
            sUtil.wrapRouteHandlers(route, app);
            app.use(route.path, route.router);
        });
    }).then(() => {
        sUtil.setErrorHandler(app);
        return BBPromise.resolve(app);
    });
}
function loadRoutes ( app ) { return fs . readdirAsync ( ` ${ __dirname } ` ) . map ( ( fname ) => { return BBPromise . try ( ( ) => { if ( ! / \.js$ / . test ( fname ) ) { return undefined ; } const route = require ( ` ${ __dirname } ${ fname } ` ) ; return route ( app ) ; } ) . then ( ( route ) => { if ( route === undefined ) { return undefined ; } if ( route . constructor !== Object || ! route . path || ! route . router || ! ( route . api_version || route . skip_domain ) ) { throw new TypeError ( ` ${ fname } ` ) ; } if ( route . path [ 0 ] !== '/' ) { route . path = ` ${ route . path } ` ; } if ( route . path [ route . path . length - 1 ] !== '/' ) { route . path = ` ${ route . path } ` ; } if ( ! route . skip_domain ) { route . path = ` ${ route . api_version } ${ route . path } ` ; } sUtil . wrapRouteHandlers ( route , app ) ; app . use ( route . path , route . router ) ; } ) ; } ) . then ( ( ) => { sUtil . setErrorHandler ( app ) ; return BBPromise . resolve ( app ) ; } ) ; }
Loads all routes declared in routes/ into the app
 @param {Application} app the application object to load routes into
 @return {bluebird} a promise resolving to the app object
Loads all routes declared in routes / into the app @param { Application } app the application object to load routes into @return { bluebird } a promise resolving to the app object
// get the list of files in routes/ // ... and then load each route // but only if it's a js file // import the route file // check that the route exports the object we need // normalise the path to be used as the mount point // wrap the route handlers with Promise.try() blocks // all good, use that route // catch errors // route loading is now complete, return the app object