space-lb2

messaging

space-node

This documentation is deprecated
Please see the new LoopBack documentation site.

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Dup lb article

Include Page
_LB Prerequisites
_LB Prerequisites

Excerpt Include
Create a simple API
Create a simple API
nopaneltrue

div
stylebox-shadow: 10px 10px 5px #888888; font-size: 130%; border: 1px solid green; width: 300px; padding: 10px; float: right; border-radius: 3px; margin: 10px 50px 10px 10px;

Because LoopBack is built on Express, you can add custom routes just as you do in Express.


Table of Contents

Break

In this part of the tutorial, you're going to add a new custom route.

Info

If you followed the previous steps in the tutorial, skip down to Introducing boot scripts.

If you're just jumping in, follow the steps below to catch up...

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

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

Introducing boot scripts

When a LoopBack application starts (or "bootstraps"), it runs the scripts in the /server/boot directory, known as boot scripts.  By default, LoopBack loads boot scripts in alphabetical order.   

The standard scaffolded LoopBack application created by the application generator contains the following standard boot scripts (in /server/boot) that perform basic initialization:

  • authentication.js - Enables authentication for the application by calling app.enableAuth().
  • root.js - Defines a root route to / that returns server status using loopback.status() middleware.  You already encountered this in the previous step, when you renamed this file so your app could serve static content.

For more information on boot scripts, see Defining boot scripts.

Add a new boot script

For example, add a new boot script named routes.js in /server/boot directory, with this code:

Code Block
title/server/boot/routes.js
module.exports = function(app) {
  // Install a "/ping" route that returns "pong"
  app.get('/ping', function(req, res) {
    res.send('pong');
  });
}

As an aside, you could have just as well used Express router middleware instead, like this:

Code Block
title/server/boot/routes.js
module.exports = function(app) {
  var router = app.loopback.Router();
  router.get('/ping', function(req, res) {
    res.send('pongaroo');
  });
  app.use(router);
}

In fact you can also add routes right in server.js using the Express API.  For example, add this call to app.use() just before the call to app.start():

Code Block
titleserver/server.js
...
app.use('/express-status', function(req, res, next) {
  res.json({ running: true });
});

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

The point is that a LoopBack application can easily do all the things that an Express application can.  If you're familiar with Express, this will make LoopBack easier to learn and use.

Run the boot script

Now, run the application again:

Code Block
$ node .

Load http://0.0.0.0:3000/ping.  You'll see "pong" as the response. 

Next step

Check out Run in a cluster to see how to run the application in a multi-process cluster.

 

...