Redirecting to http://loopback.io/doc/en/lb2/Add-a-custom-Express-route.html ...
In this part of the tutorial, you're going to add a new custom route.
Get the app (in the state following the last article) from GitHub and install all its dependencies:
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
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
/server/boot directory, with this code:
As an aside, you could have just as well used Express router middleware instead, like this:
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
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:
Load http://0.0.0.0:3000/ping. You'll see "pong" as the response.