Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run multiple StrongLoop LoopBack apps on the same server?

I'm currently running two StrongLoop LoopBack apps (Nodejs apps) on a single server with different ports. Both apps were created using slc lb project and slc lb model from the command line.

Is it possible to run these apps on a single ports with different path and/or subdomain? If it is, how do I do that on a Linux machine?

Example:

http://api.server.com:3000/app1/ for first app.

http://api.server.com:3000/app2/ for second app.

thanks.

like image 966
Muhammad Baja Aksha Avatar asked Mar 27 '14 04:03

Muhammad Baja Aksha


People also ask

How do I run my application under control of StrongLoop Process Manager?

To run your application under control of StrongLoop Process Manager use the slc start command. Doing this enables you to profile the app and monitor app metrics to help find memory leaks and optimize performance. See Profiling and Monitoring app metrics for more information.

What tools do I need to create a Loopback application?

The StrongLoop command-line tool, slc, for creating LoopBack applications and for running and managing Node applications. LoopBack Angular command line tools. See AngularJS JavaScript SDK for details. StrongLoop Arc, a graphical tool suite for the API lifecycle, including tools for building, profiling and monitoring Node apps.

What is the StrongLoop command line tool?

The StrongLoop command-line tool, slc, for creating LoopBack applications and for running and managing Node applications. LoopBack Angular command line tools. See AngularJS JavaScript SDK for details.

When should I migrate from loopback 3 to loopback 4?

We urge all LoopBack 3 users to migrate their applications to LoopBack 4 as soon as possible. Learn more about LoopBack's long term support policy. In general, when you are developing an application, use the node command to run it. This enables you to see stack traces and console output immediately.


2 Answers

Since LoopBack applications are regular Express applications, you can mount them on a path of the master app.

var app1 = require('path/to/app1');
var app2 = require('path/to/app2');

var root = loopback(); // or express();
root.use('/app1', app1);
root.use('/app2', app2);
root.listen(3000);

The obvious drawback is high runtime coupling between app1 and app2 - whenever you are upgrading either of them, you have to restart the whole server (i.e. both of them). Also a fatal failure in one app brings down the whole server.

The solution presented by @fiskeben is more robust, since each app is isolated.

On the other hand, my solution is probably easier to manage (you have only one Node process instead of nginx + per-app Node processes) and also allows you to configure middleware shared by both apps.

var root = loopback();
root.use(express.logger());
// etc.

root.use('/app1', app1);
root.use('/app2', app2);
root.listen(3000);
like image 179
Miroslav Bajtoš Avatar answered Oct 04 '22 16:10

Miroslav Bajtoš


You would need some sort of proxy in front of your server, for example nginx. nginx will listen to a port (say, 80) and redirect incoming requests to other servers on the machine based on some rules you define (hostname, path, headers, etc).

I'm no expert on nginx but I would configure it something like this:

server {
  listen: 80;
  server_name api.server.com;
  location /app1 {
    proxy_pass http://localhost:3000
  }
  location /app2 {
    proxy_pass http://localhost:3001
  }
}

nginx also supports passing query strings, paths and everything else, but I'll leave it up to you to put the pieces together :)

Look at the proxy server documentation for nginx.

like image 36
fiskeben Avatar answered Oct 04 '22 17:10

fiskeben