modules/orionode/index.js
function tryLoadRouter(endpoint, options) {
	const args = [];
	var isEndpoint = typeof endpoint.endpoint === 'string';
	if (isEndpoint) {
		args.push(endpoint.endpoint);
		args.push(responseTime({digits: 2, header: "X-Total-Response-Time", suffix: true}));
	}
	if (endpoint.authenticated) {
		args.push(options.authenticate);
		if (endpoint.checkAuthenticated === undefined || endpoint.checkAuthenticated) {
			args.push(checkAuthenticated);
		}
	}
	if (isEndpoint) {
		args.push(options.basicMiddleware);
	}
	if (isEndpoint && csrf && (endpoint.checkCSRF === undefined || endpoint.checkCSRF)) { // perform CSRF by default
		args.push(csrf);
		args.push(function(req, res, next) {
			var preamble = options.configParams.get("orion_cookies_name_premable") || "";
			var tokenCookie = preamble + 'x-csrf-token';
			if (!req.cookies[tokenCookie]) {
				var cookieOptions;
				var cookiesPath = options.configParams.get("orion_cookies_path");
				if (cookiesPath) {
					cookieOptions = {path: cookiesPath};
				}
				res.cookie(tokenCookie, req.csrfToken(), cookieOptions);
			}
			next();
		});
	}
	if (endpoint.checkAccess) {
		args.push(checkAccessRights);
	}
	try {
		const mod = require(endpoint.module);
		let fn = null;
		if (typeof mod.router === 'function') {
			fn = mod.router;
		} else if(typeof mod === 'function') {
			fn = mod;
		} else {
			logger.log("Endpoint did not provide the API 'router' function: " + JSON.stringify(endpoint, null, '\t'));
			return;
		}
		if(fn) {
			const router = fn(options);
			if (!router) {
				return; //endpoint does not want to take part in routing, quit
			}
			args.push(router);
			options.app.use.apply(options.app, args);
		}
	} catch (err) {
		logger.error("Failed to load module: " + err.message + "..." + err.stack);
	}
}
function tryLoadRouter(endpoint, options) {
	const args = [];
	var isEndpoint = typeof endpoint.endpoint === 'string';
	if (isEndpoint) {
		args.push(endpoint.endpoint);
		args.push(responseTime({digits: 2, header: "X-Total-Response-Time", suffix: true}));
	}
	if (endpoint.authenticated) {
		args.push(options.authenticate);
		if (endpoint.checkAuthenticated === undefined || endpoint.checkAuthenticated) {
			args.push(checkAuthenticated);
		}
	}
	if (isEndpoint) {
		args.push(options.basicMiddleware);
	}
	if (isEndpoint && csrf && (endpoint.checkCSRF === undefined || endpoint.checkCSRF)) { // perform CSRF by default
		args.push(csrf);
		args.push(function(req, res, next) {
			var preamble = options.configParams.get("orion_cookies_name_premable") || "";
			var tokenCookie = preamble + 'x-csrf-token';
			if (!req.cookies[tokenCookie]) {
				var cookieOptions;
				var cookiesPath = options.configParams.get("orion_cookies_path");
				if (cookiesPath) {
					cookieOptions = {path: cookiesPath};
				}
				res.cookie(tokenCookie, req.csrfToken(), cookieOptions);
			}
			next();
		});
	}
	if (endpoint.checkAccess) {
		args.push(checkAccessRights);
	}
	try {
		const mod = require(endpoint.module);
		let fn = null;
		if (typeof mod.router === 'function') {
			fn = mod.router;
		} else if(typeof mod === 'function') {
			fn = mod;
		} else {
			logger.log("Endpoint did not provide the API 'router' function: " + JSON.stringify(endpoint, null, '\t'));
			return;
		}
		if(fn) {
			const router = fn(options);
			if (!router) {
				return; //endpoint does not want to take part in routing, quit
			}
			args.push(router);
			options.app.use.apply(options.app, args);
		}
	} catch (err) {
		logger.error("Failed to load module: " + err.message + "..." + err.stack);
	}
}
function tryLoadRouter ( endpoint , options ) { const args = [ ] ; var isEndpoint = typeof endpoint . endpoint === 'string' ; if ( isEndpoint ) { args . push ( endpoint . endpoint ) ; args . push ( responseTime ( { digits : 2 , header : "X-Total-Response-Time" , suffix : true } ) ) ; } if ( endpoint . authenticated ) { args . push ( options . authenticate ) ; if ( endpoint . checkAuthenticated === undefined || endpoint . checkAuthenticated ) { args . push ( checkAuthenticated ) ; } } if ( isEndpoint ) { args . push ( options . basicMiddleware ) ; } if ( isEndpoint && csrf && ( endpoint . checkCSRF === undefined || endpoint . checkCSRF ) ) { // perform CSRF by default args . push ( csrf ) ; args . push ( function ( req , res , next ) { var preamble = options . configParams . get ( "orion_cookies_name_premable" ) || "" ; var tokenCookie = preamble + 'x-csrf-token' ; if ( ! req . cookies [ tokenCookie ] ) { var cookieOptions ; var cookiesPath = options . configParams . get ( "orion_cookies_path" ) ; if ( cookiesPath ) { cookieOptions = { path : cookiesPath } ; } res . cookie ( tokenCookie , req . csrfToken ( ) , cookieOptions ) ; } next ( ) ; } ) ; } if ( endpoint . checkAccess ) { args . push ( checkAccessRights ) ; } try { const mod = require ( endpoint . module ) ; let fn = null ; if ( typeof mod . router === 'function' ) { fn = mod . router ; } else if ( typeof mod === 'function' ) { fn = mod ; } else { logger . log ( "Endpoint did not provide the API 'router' function: " + JSON . stringify ( endpoint , null , '\t' ) ) ; return ; } if ( fn ) { const router = fn ( options ) ; if ( ! router ) { return ; //endpoint does not want to take part in routing, quit } args . push ( router ) ; options . app . use . apply ( options . app , args ) ; } } catch ( err ) { logger . error ( "Failed to load module: " + err . message + "..." + err . stack ) ; } }
Tries to load the router from an endpoint
@param {{?}} endpoint The metadata for the endpoint to load
@param {{?}} options The map of options from the server load
Tries to load the router from an endpoint