LoopBack 101: Express.js on Steroids

LoopBack 101: Express.js on Steroids

LoopBack is a comprehensive Node.js web framework with a rich command-line scaffolding and a web API explorer: strongloop.com/node-js/loopback. The framework is maintained by StrongLoop which is also the gate-keeper of Express.js.

This concise tutorial will illustrate how to get started with LoopBack and the common traits between LoopBack and Express.js. This text is from my new book Pro Express.js by Apress which you can already start reading in Apress Alpha or pre-order on Amazon.com and other bookstores.

To install the LoopBack command-line tool, run:

$ npm install -g strongloop@2.9.1

To create the boilerplate app, run this command and answer the subsequent questions:

$ slc loopback

At the end, the command-line tool will show you some of the available options:

  • Change directory to your app:
       $ cd loopback
    
  • Create a model in your app:
       $ slc loopback:model
    
  • Optional: Enable StrongOps monitoring:
       $ slc strongops
    
  • Run the app:
       $ slc run .
    

Let’s create a model Book with property “name” of string, plural Books, memory store, and REST API[wfm2] (slc will ask for inputs):

$ cd loopback
$ slc loopback:model

After you’re finished with the model, run:

$ slc run

Then go to http://localhost:3000/explorer in your browser (see the figure) and click Books to explore the API.

LoopBack’s explorer is a web interface for APIs.
LoopBack’s explorer is a web interface for APIs.

To demonstrate that LoopBack is built on top of Express.js, edit proexpressjs/ch18/loopback/common/models/book.js as follows:

module.exports = function(Book) {
  Book.buy = function(code, cb) {
    cb(null, 'Processing... ' + code);
  }

  Book.remoteMethod('buy', {accepts: [{http: function(ctx) {
    // HTTP request object as provided by Express.js
    var request = ctx.req;
    console.log(request.param('code'), request.ip, request.hostname)
    return request.param('code');
  }}],
      returns: {arg: 'response', type: 'string'}
    }
  );
};

The request(ctx.req) object is the Express.js request object. We can call the request.param() method (among other Express.js methods, such as request.ip() and request.hostname()) to extract the code parameter.

The client request is as follows:

$ curl http://localhost:3000/api/Books/Buy -X POST -d "code=1"

The client response is:

{"response":"Processing... 1"}%

The server logs are:

1 127.0.0.1 localhost

Don’t forget to restart the server with $ slc run every time you make changes to the source file.

Another place where you can use your Express.js skills is proexpressjs/ch18/loopback/server/server.js; for example:

app.use(middleware());

If you really want to become an expert in Express.js in the fastest and the most painless way, then get you copy of Pro Express.js now.

For those of you who read Express.js Guide, the Pro Express.js book is 150% better with more recent versions, more extensive comments and better examples.

Author: Azat

Techies, entrepreneur, 20+ years in tech/IT/software/web development expert: NodeJS, JavaScript, MongoDB, Ruby on Rails, PHP, SQL, HTML, CSS. 500 Startups (batch Fall 2011) alumnus. http://azat.co http://github.com/azat-co

6 thoughts on “LoopBack 101: Express.js on Steroids”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.