Search code examples
node.jsrestexpressloopbackjsstrongloop

Using Non-Rest Calls with Loopback (Strongloop)


We're using Loopback for our REST apis and would like to implement some standard Node Express-like calls through the same instance that do not get automatically routed through the Loopback framework. How can we add a new, separate route without disturbing the Loopback routing? Here's the standard Loopback startup code:

var loopback = require('loopback');
var boot = require('loopback-boot');

var app = module.exports = loopback();

// Bootstrap the application, configure models, datasources and middleware.
// Sub-apps like REST API are mounted via boot scripts.
boot(app, __dirname);

app.start = function() {
  // start the web server
  return app.listen(function() {
    app.emit('started');
    console.log('Web server listening at: %s', app.get('url'));
  });
};

// start the server if `$ node server.js`
if (require.main === module) {
  app.start();
}

Solution

  • Just add it via middleware in server/server.js as you would normally do in a typical Express app.

    ...
    // Bootstrap the application, configure models, datasources and middleware.
    // Sub-apps like REST API are mounted via boot scripts.
    boot(app, __dirname);
    
    app.use('/', function(req, res) {
      res.send('hello world')
    });
    ....