In LoopBack, a Node function attached to a custom REST endpoint is called a remote method.

Page Contents

  In this section you’re going to add a custom remote method to your API.

Get the app (in the state following the last article) from GitHub and install all its dependencies:

$ git clone https://github.com/strongloop/loopback-getting-started.git
$ cd loopback-getting-started
$ git checkout step2
$ npm install

Add a remote method

Follow these steps:

  1. Look in your application’s /common/models directory.  You’ll notice there are coffee-shop.js and coffee-shop.json files there.  

</div>

  1. Open coffee-shop.js in your favorite editor.  By default, it contains an empty function: 

    module.exports = function(CoffeeShop) {};
    
  2. Add the following code to this function to extend the model’s behavior with a remote method, so it looks as shown here:

    module.exports = function(CoffeeShop) {
      CoffeeShop.status = function(cb) {
        var currentDate = new Date();
        var currentHour = currentDate.getHours();
        var OPEN_HOUR = 6;
        var CLOSE_HOUR = 20;
        console.log('Current hour is %d', currentHour);
        var response;
        if (currentHour >= OPEN_HOUR && currentHour < CLOSE_HOUR) {
          response = 'We are open for business.';
        } else {
          response = 'Sorry, we are closed. Open daily from 6am to 8pm.';
        }
        cb(null, response);
      };
      CoffeeShop.remoteMethod(
        'status', {
          http: {
            path: '/status',
            verb: 'get'
          },
          returns: {
            arg: 'status',
            type: 'string'
          }
        }
      );
    };
    

    This defines a simple remote method called “status” that takes no arguments, and checks the time and returns a JSON status message that says either “Open for business” or “Sorry we are closed”, depending on the current time.

    Of course, in practice you can do much more interesting and complex things with remote methods such as manipulating input data before persisting it to a database.  You can also change the route where you call the remote method, and define complex arguments and return values.  See Remote methods for all the details.

  3. Save the file.

Try the remote method

  1. Back in the application root directory, run the app: 

    $ node .

  2. Go to http://localhost:3000/explorer to see API Explorer.  Then click on CoffeeShops and you’ll see there is a new REST endpoint, GET/CoffeeShop/status that calls the remote method.

    </figure>  

  3. Click Try it Out! You’ll see the result of calling your remote method :
    {
      "status": "Open for business."
    }
    

That’s how easy it is to add remote methods with LoopBack! 

For more information, see Remote methods.

Executing create, retrieve, update, and delete methods in a remote method

The status remote method is trivial, but a remote method can also access any of the standard model create, retrieve, update, and delete methods to perform data processes and validation.  Here is a simple example (this is not in the loopback-getting-started repository):

module.exports = function(CoffeeShop) {
...
  CoffeeShop.getName = function(shopId, cb) {
    CoffeeShop.findById( shopId, function (err, instance) {
        var response = "Name of coffee shop is " + instance.name;
        cb(null, response);
        console.log(response);
    });
  }
...
  CoffeeShop.remoteMethod (
        'getName',
        {
          http: {path: '/getname', verb: 'get'},
          accepts: {arg: 'id', type: 'number', http: { source: 'query' } },
          returns: {arg: 'name', type: 'string'}
        }
    );
}

Then, if you access the remote method at, for example:

http://0.0.0.0:3000/api/CoffeeShops/getname?id=1

You’ll get the response:

{
  "name": "Name of coffee shop is Bel Cafe"
}

Next: In Add a static web page, you’ll add Express middleware to serve static client assets such as HTML/CSS, images, and JavaScript.