$ git clone https://github.com/strongloop/loopback-example-app-logic.git
$ cd loopback-example-app-logic
$ npm install
$ node .
# then in a different tab, run ./bin/remote-method-request or ./bin/datetime-request
In this example, we demonstrate remote methods, remote hooks, model operation hooks, boot scripts, middleware, and email-connector as solutions for integrating user-defined logic into a LoopBack application.
Tutorials:
Knowledge:
Application information:
- Name:
loopback-example-app-logic
- Directory to contain the project:
loopback-example-app-logic
$ lb app loopback-example-app-logic
... # follow the prompts
$ cd loopback-example-app-logic
Model information:
- Name:
car
- Datasource:
db (memory)
- Base class:
PersistedModel
- Expose via REST:
Yes
- Custom plural form: Leave blank
- Properties
make
- String
- Not required
model
- String
- Not required
- Datasource:
$ lb model car
... # follow the prompts
Define a remote method in car.js
.
The remote method takes a "sound" and repeats it three times.
Test it by starting the server (using node .
) and running curl -XPOST localhost:3000/api/cars/rev-engine -H 'content-type:application/json' -d '{"sound":"vroom"}'
.
If you are using Windows, single quotes are treated as backticks in
cmd
. This means you will have to modify thecurl
command to use and escape double quotes instead:curl -XPOST localhost:3000/api/cars/rev-engine -H "content-type:application/json" -d "{\"sound\":\"vroom\"}"
.
You should see:
...
{"engineSound":"vroom vroom vroom"}
Define a remote method before hook in car.js
.
The second parameter
unused
must be provided for legacy reasons. You may simply ignore it, but you must declare it to ensurenext
is the third parameter. This is a side effect of inheriting from thejugglingdb
library.
context
contains the Express request and response objects (ie.context.req
andcontext.res
).
This method is triggered right before revEngine
is called and prints a message to the console.
Restart the server.
$ ./bin/remote-method-request
You should see:
...
Putting in the car key, starting the engine.
Define a remote method after hook in car.js
.
This method is triggered after revEngine
finishes execution and prints a message to the console.
Restart the server.
$ ./bin/remote-method-request
You should see:
...
Turning off the engine, removing the key.
Create print-models.js
in the boot
directory.
NOTE: The
app
argument is provided by LoopBack. You can use it to access the application context, which is required when you want to retrieve models, configs, and so on.
To use asynchronous boot scripts, you have to modify boot
to take callback. You will also need to provide an additional callback
argument in your boot scripts.
Restart the server.
In the server output, you should see:
...
Models: [ 'User', 'AccessToken', 'ACL', 'RoleMapping', 'Role', 'car' ]
...
Define a model operation hook in car.js
.
Copy the create-car.js
script to the server/boot
directory.
$ cp examples/async-boot-script/create-car.js server/boot/
Restart the server.
You should see:
...
About to save a car instance: { make: 'honda', model: 'civic' }
A `car` instance has been created from a boot script: { make: 'honda', model: 'civic', id: 1 }
...
This model operation hook is triggered before saving any car
model instance.
Many other operation hooks are available, such as
access
,before save
,after save
,before delete
, andafter delete
. See the model operation hooks documentation for more information.
Create the middleware
directory to store middleware
files.
$ mkdir server/middleware
Create the tracker
middleware to respond with
the request processing time.
Register the tracker
middleware in middleware.json
.
We register
tracker
in theinitial
phase because we want it configured before other middleware. See the official middleware phases documentation.
Restart the server.
$ ./bin/remote-method-request
You should see:
...
The request processing time is 28.472051 ms.
Your time will be different.
Create the datetime
middleware, which responds with the current date and time when a request is made to localhost:3000/datetime
.
Register the datetime
middleware in middleware.json
.
Create a shell script to test the middleware.
Restart the server.
$ ./bin/datetime-request
You should see:
...
{"datetime":"2015-01-14T22:54:35.708Z"}
Your date and time will be different.
How do you send email?
- Configure an email datasource
- Map the built-in
Email
model to the the email datasource - Send an email using the configured model
Notes:
- This example contains a boot script that sends an email every time you start the application.
- Be sure to use YOUR email configurations in
datasources.json
- You will need to configure
boot()
inserver.js
to take a callback for the application to start up properly because we use an asynchronous boot script for this example