LoopBack 101: Express.js on Steroids

LoopBack 101: Express.js on Steroids
Courses on Node.js, React
and JavaScript
Become an expert with my comprehensive
Node.js, React.js and JavaScript courses.
Learn Full Stack Javascript →


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.

[Sidenote]

Reading blog posts is good, but watching video courses is even better because they are more engaging.

A lot of developers complained that there is a lack of affordable quality video material on Node. It's distracting to watch to YouTube videos and insane to pay $500 for a Node video course!

Go check out Node University which has FREE video courses on Node: node.university.

[End of sidenote]

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.

Courses on Node.js,
React and JavaScript
Become an expert with my comprehensive
Node.js, React.js and JavaScript courses.
Learn Full Stack Javascript →

--
Azat Mardan
Azat Mardan avatar
https://www.linkedin.com/in/azatm
To contact Azat, the main author of this blog, submit the contact form.

Also, make sure to get 3 amazing resources to FREE when you sign up for the newsletter.
Simple.
Easy.
No commitment.

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

  1. Pingback: Error Handling and Running an Express.js App

  2. Pingback: Express.js Security Tips

  3. Pingback: Quick Announcement and a Favor

Leave a Reply

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