Strong-remoting makes Node functions remotely accessible over the network.Note: This page was generated from the strong-remoting/README.md.

See also:

strong-remoting

This module is in Active LTS mode, new features are no longer accepted.(See Module Long Term Support Policybelow.)

LoopBack 3 users looking for new features are encouraged to upgradeto LoopBack 4. Refer toloopback-next#1849for more information on how to upgrade.

If you are building directly on top of strong-remoting without LoopBack,then please open a new GitHub issueto discuss the specifics.

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.Also see the official StrongLoop documentation.

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.

Module Long Term Support Policy

This module adopts the Module Long Term Support (LTS) policy, with the following End Of Life (EOL) dates:

VersionStatusPublishedEOL
strong-remoting@3Active LTSDec 2016Dec 2019
strong-remoting@2End-of-LifeJul 2014Apr 2019

Learn more about our LTS plan in docs.

Installation

  1. $ npm install strong-remoting

Quick start

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

  1. // Create a collection of remote objects.
  2. var remoting = require('../');
  3. var SharedClass = remoting.SharedClass
  4. var remotes = remoting.create();
  5. // define a class-like object (or constructor)
  6. function User() {
  7. }
  8. User.greet = function (fn) {
  9. fn(null, 'hello, world!');
  10. }
  11. // create a shared class to allow strong-remoting to map
  12. // http requests to method invocations on your class
  13. var userSharedClass = new SharedClass('user', User);
  14. // Tell strong-remoting about your greet method
  15. userSharedClass.defineMethod('greet', {
  16. isStatic: true, // not an instance method
  17. returns: [{
  18. arg: 'msg',
  19. type: 'string' // define the type of the callback arguments
  20. }]
  21. });
  22. // Expose it over the REST transport.
  23. require('http')
  24. .createServer(remotes.handler('rest'))
  25. .listen(3000);

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

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

Result:

  1. {
  2. "msg": "hello world"
  3. }

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).

  1. // Do something before any hook is executed
  2. remotes.authorization = function(ctx, next) {
  3. if(checkContext(ctx)) {
  4. // allow
  5. next();
  6. } else {
  7. // deny
  8. var err = new Error('denied!');
  9. err.statusCode = 401;
  10. next(err);
  11. }
  12. }
  13. // Do something before our `user.greet` example, earlier.
  14. remotes.before('user.greet', function (ctx, next) {
  15. if((ctx.req.param('password') || '').toString() !== '1234') {
  16. next(new Error('Bad password!'));
  17. } else {
  18. next();
  19. }
  20. });
  21. // Do something before any `user` method.
  22. remotes.before('user.*', function (ctx, next) {
  23. console.log('Calling a user method.');
  24. next();
  25. });
  26. // Do something before a `dog` instance method.
  27. remotes.before('dog.prototype.*', function (ctx, next) {
  28. var dog = this;
  29. console.log('Calling a method on "%s".', dog.name);
  30. next();
  31. });
  32. // Do something after the `speak` instance method.
  33. // NOTE: you cannot cancel a method after it has been called.
  34. remotes.after('dog.prototype.speak', function (ctx, next) {
  35. console.log('After speak!');
  36. next();
  37. });
  38. // Do something before all methods.
  39. remotes.before('**', function (ctx, next, method) {
  40. console.log('Calling:', method.name);
  41. next();
  42. });
  43. // Modify all returned values named `result`.
  44. remotes.after('**', function (ctx, next) {
  45. ctx.result += '!!!';
  46. next();
  47. });

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:

  1. // accepting a callback argument
  2. remotes.after('dog.prototype.speak', function(ctx, next) {
  3. console.log('After speak!');
  4. next();
  5. });
  6. // returning a promise
  7. remotes.after('dog.prototype.speak', function(ctx) {
  8. console.log('After speak!');
  9. return Promise.resolve();
  10. });

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:

  1. // Create a Collection.
  2. var remotes = require('strong-remoting').create();
  3. // Share some fs module code.
  4. var fs = remotes.exports.fs = require('fs');
  5. // Specifically export the `createReadStream` function.
  6. fs.createReadStream.shared = true;
  7. // Describe the arguments.
  8. fs.createReadStream.accepts = {arg: 'path', type: 'string'};
  9. // Describe the stream destination.
  10. fs.createReadStream.http = {
  11. // Pipe the returned `Readable` stream to the response's `Writable` stream.
  12. pipe: {
  13. dest: 'res'
  14. }
  15. };
  16. // Expose the Collection over the REST Adapter.
  17. require('http')
  18. .createServer(remotes.handler('rest'))
  19. .listen(3000);

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

  1. $ curl http://localhost:3000/fs/createReadStream?path=some-file.txt

Tags: readme