space-lb2

messaging

space-node

This documentation is deprecated
Please see the new LoopBack documentation site.
Skip to end of metadata
Go to start of metadata

    strong-remoting

    Overview

    Objects (and, therefore, data) in Node applications commonly need to be accessible by other Node processes, browsers, and even mobile clients. Strong remoting:

    • Makes local functions remotable, exported over adapters.
    • Supports multiple transports, including custom transports.
    • Manages serialization to JSON and deserialization from JSON.
    • Supports multiple client SDKs, including mobile clients.

    Client SDK support

    For higher-level transports, such as REST and Socket.IO, existing clients will work well. If you want to be able to swap out your transport, use one of our supported clients. The same adapter model available on the server applies to clients, so you can switch transports on both the server and all clients without changing your application-specific code.

    Supported versions

    Current Long Term Support
    3.x 2.x

    Learn more about our LTS plan in docs.

    Installation

    Quick start

    The following example illustrates how to set up a basic strong-remoting server with a single remote method, user.greet.

    Then, invoke User.greet() easily with curl (or any HTTP client)!

    $ curl http://localhost:3000/user/greet?str=hello
    

    Result:

    {
      "msg": "hello world"
    }
    

    Concepts

    Remote objects

    Most Node applications expose a remotely-available API. Strong-remoting enables you to build your app in vanilla JavaScript and export remote objects over the network the same way you export functions from a module. Since they’re just plain JavaScript objects, you can always invoke methods on your remote objects locally in JavaScript, whether from tests or other, local objects.

    Remote object collections

    Collections that are the result of require(‘strong-remoting’).create() are responsible for binding their remote objects to transports, allowing you to swap out the underlying transport without changing any of your application-specific code.

    Adapters

    Adapters provide the transport-specific mechanisms to make remote objects (and collections thereof) available over their transport. The REST adapter, for example, handles an HTTP server and facilitates mapping your objects to RESTful resources. Other adapters, on the other hand, might provide a less opinionated, RPC-style network interface. Your application code doesn’t need to know what adapter it’s using.

    Hooks

    Hooks enable you to run code before remote objects are constructed or methods on those objects are invoked. For example, you can prevent actions based on context (HTTP request, user credentials, and so on).

    Hooks accept an asynchronous handler function that is called for every request. This handler function signals the completion either by accepting a callback argument or returning a promise. For example:

    See the before-after example for more info.

    Streams

    Strong-remoting supports methods that expect or return Readable and Writeable streams. This enables you to stream raw binary data such as files over the network without writing transport-specific behavior.

    For example, the following code exposes a method of the fs Remote Object, fs.createReadStream, over the REST adapter:

    Then you can invoke fs.createReadStream() using curl as follows:

    ...

    • No labels