Configure application-specific extensions for the application router.
Instead of starting the application router directly, you can configure your XS advanced application to use its own start script. You can also use the application router as a regular Node.js package.
var approuter = require('@sap/approuter'); var ar = approuter(); ar.start();
The application router uses the connect framework, for more information, see Connect framework in the Related Links below. You can reuse all injected “connect” middleware within the application router, for example, directly in the application start script:
var approuter = require('@sap/approuter'); var ar = approuter(); ar.beforeRequestHandler.use('/my-ext', function myMiddleware(req, res, next) { res.end('Request handled by my extension!'); }); ar.start();
To facilitate troubleshooting, always provide a name for your custom middleware.
The path argument is optional. You can also chain use calls.
var approuter = require('@sap/approuter'); var morgan = require('morgan'); var ar = approuter(); ar.beforeRequestHandler .use(morgan('combined')) .use('/my-ext', function myMiddleware(req, res, next) { res.end('Request handled by my extension!'); }); ar.start();
The application router defines the following slots where you can insert custom middleware:
-
first- right after the connect application is created, and before any application router middleware. At this point security checks are not performed yet.This is good place for infrastructure logic, for example, logging and monitoring.
-
beforeRequestHandler- before standard application router request handling, that is static resource serving or forwarding to destinations.This is a good place to handle custom REST API requests.
-
beforeErrorHandler- before standard application router error handling.This is a good place to capture or customize error handling.
If your middleware does not complete the request processing, call next to return control to the application router middleware, as illustrated in the following example:
ar.beforeRequestHandler.use('/my-ext', function myMiddleware(req, res, next) { res.setHeader('x-my-ext', 'passed'); next(); });
An extension is defined by an object with the following properties:
-
insertMiddleware- describes the middleware provided by this extension-
first,beforeRequestHandler, andbeforeErrorHandlerAn array of middleware, where each one can be either of the following elements:
-
A middleware function (invoked on all requests)
-
An object with the following properties:
-
pathHandle requests only for this path
-
handlerThe middleware function to invoke
-
-
-
Example Approuter Extension Configuration (
my-ext.js)module.exports = { insertMiddleware: { first: [ function logRequest(req, res, next) { console.log('Got request %s %s', req.method, req.url); } ], beforeRequestHandler: [ { path: '/my-ext', handler: function myMiddleware(req, res, next) { res.end('Request handled by my extension!'); } } ] } };
The extension configuration can be referenced in the corresponding application's start script, as illustrated in the following example:
var approuter = require('@sap/approuter'); var ar = approuter(); ar.start({ extensions: [ require('./my-ext.js') ] });
By default the application router handles its command-line parameters, but that can be customized as well.
An <approuter> instance provides the property cmdParser that is a commander instance. It is configured with the standard application router command line options. You can add custom options like this:
var approuter = require('@sap/approuter'); var ar = approuter(); var params = ar.cmdParser // add here custom command line options if needed .option('-d, --dummy', 'A dummy option') .parse(process.argv); console.log('Dummy option:', params.dummy);
To disable the handling of command-line options in the application router, reset the ar.cmdParser property to “false”, as illustrated in the following example:
ar.cmdParser = false;
Related Information