Search code examples
loopbackjs

Loopback doesn't create rest api for container


Following docs here https://loopback.io/doc/en/lb2/Storage-component.html#using-javascript

I added the file storage component.

Here is server.js

'use strict';

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

var app = module.exports = loopback();

app.start = function() {
  // start the web server
  return app.listen(function() {
    app.emit('started');
    var baseUrl = app.get('url').replace(/\/$/, '');
    console.log('Web server listening at: %s', baseUrl);
    if (app.get('loopback-component-explorer')) {
      var explorerPath = app.get('loopback-component-explorer').mountPath;
      console.log('Browse your REST API at %s%s', baseUrl, explorerPath);
    }
  });
};

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

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

// File upload datasource
var ds = loopback.createDataSource({
  connector: require('loopback-component-storage'),
  provider: 'filesystem',
  root: path.join(__dirname, 'storage'),
});

var container = ds.createModel('container');

container.getContainers(function(res) {
  console.log(res);
});

But going to http://localhost:3000/api/containers gives back 404 and no apis appeared in explorer.


Solution

  • I was able to create the model with lb model selecting the datasource for storage.

    Then the rest apis are provided.