Page Contents

In addition to defining routes at the Controller and operation level, you can also customize them at the application level.

Implementing HTTP redirects

Both RestServer and RestApplication classes provide API for registering routes that will redirect clients to a given URL.

Example use:


import {RestApplication} from '@loopback/rest';

export class MyApplication extends RestApplication {
  constructor(options: ApplicationConfig = {}) {

    // Use the default status code 303 See Other
    this.redirect('/', '/home');

    // Specify a custom status code 301 Moved Permanently
    this.redirect('/stats', '/status', 301);

Mounting an Express Router

If you have an existing Express application that you want to use with LoopBack 4, you can mount the Express application on top of a LoopBack 4 application. This way you can mix and match both frameworks, while using LoopBack as the host. You can also do the opposite and use Express as the host by mounting LoopBack 4 REST API on an Express application. See Creating an Express Application with LoopBack REST API for the tutorial.

Mounting an Express router on a LoopBack 4 application can be done using the mountExpressRouter function provided by both RestApplication and RestServer.

Example use:


import {Request, Response} from 'express';
import express from 'express';

const legacyApp = express();

// your existing Express routes
legacyApp.get('/pug', function (_req: Request, res: Response) {

export {legacyApp};


import {RestApplication} from '@loopback/rest';

const {legacyApp} = require('./express-app');

const openApiSpecForLegacyApp: RouterSpec = {
  // insert your spec here, your 'paths', 'components', and 'tags' will be used

class MyApplication extends RestApplication {
  constructor(/* ... */) {
    // ...

    this.mountExpressRouter('/dogs', legacyApp, openApiSpecForLegacyApp);

Any routes you define in your legacyApp will be mounted on top of the /dogs base path, e.g. if you visit the /dogs/pug endpoint, you’ll see Pug!.