Creating your own servers

LoopBack 4 has the concept of a Server, which you can use to create your ownimplementations of REST, SOAP, gRPC, MQTT and more. For an overview, seeServer.

Typically, you’ll want server instances that listen for traffic on one or moreports (this is why they’re called “servers”, after all). This leads into a keyconcept to leverage for creating your custom servers.

Controllers and routing

LoopBack 4 developers are strongly encouraged to use controllers for theirmodules, and this naturally leads to the concept of routing.

No matter what protocol you intend to use for your custom server, you’ll need touse some algorithm to determine which controller and function to send requestdata to, and that means you need a router.

For example, consider a “toy protocol” similar to the JSON RPC specification(but nowhere near as complete or robust).

The toy protocol will require a JSON payload with three properties:controller, method, and input.

An example request would look something like this:

  1. {
  2. "controller": "GreetController",
  3. "method": "basicHello",
  4. "input": {
  5. "name": "world"
  6. }
  7. }

You can find the code for our sample RPC server implementationover here.

Trying it out

First, install your dependencies and then start the application:

  1. npm i && npm start

Now, try it out: start the server and run a few REST requests. Feel free to usewhatever REST client you’d prefer (this example will use curl).

  1. # Basic Greeting Calls
  2. $ curl -X POST -d '{ "controller": "GreetController", "method": "basicHello" }' -H "Content-Type: application/json" http://localhost:3000/
  3. Hello, World!
  4. $ curl -X POST -d '{ "controller": "GreetController", "method": "basicHello", "input": { "name": "Nadine" } }' -H "Content-Type: application/json" http://localhost:3000/
  5. Hello, Nadine!
  6. # Advanced Greeting Calls
  7. $ curl -X POST -d '{ "controller": "GreetController", "method": "hobbyHello", "input": { "name": "Nadine" } }' -H "Content-Type: application/json" http://localhost:3000/
  8. Hello, Nadine! I heard you like underwater basket weaving.
  9. $ curl -X POST -d '{ "controller": "GreetController", "method": "hobbyHello", "input": { "name": "Nadine", "hobby": "extreme mountain biking" } }' -H "Content-Type: application/json" http://localhost:3000/
  10. Hello, Nadine! I heard you like extreme mountain biking.

While a typical protocol server would be a lot more involved in theimplementation of both its router and server, the general concept remains thesame, and you can use these tools to make whatever server you’d like.

Other considerations

Some additional concepts to add to your server could include:

  • Pre-processing of requests (changing content types, checking the request body,etc)
  • Post-processing of responses (removing sensitive/useless information)
  • Caching
  • Logging
  • Automatic creation of default endpoints
  • and more…LoopBack 4’s modularity allows for custom servers of all kinds, while stillproviding key utilities like context and injection to make your work easier.