Implementing a separated front-end and back-end for an application

My web app consists of two separate components:

  1. An API built on the Place Framework that handles requests of type /api/* for any client.
  2. A decoupled front end created with AngularJS using grunt build.

Currently, the front end communicates with the API. I want to deploy both units behind a proxy like nginx, which will route incoming requests to the appropriate component. For instance, all /web/* requests should be served from a web directory containing the client-side source files (js/html/etc.), while /api/* requests need to be proxied to my Play framework server (path information must be passed on to ensure correct responses). So, a request such as GET domain.com/api/users should internally map to GET 127.0.0.1:9000/api/users.

I've come across discussions online about this setup and would like to get your input on the best approach for deploying these components in this manner.

In the future, I envision moving towards a service-oriented architecture and further enhancing the decoupling of components.

Answer №1

Successfully implemented Play Framework + AngularJS applications and discovered the effectiveness of utilizing nginx in the process.

Nginx provides a flexible pathway for accommodating additional services as your app's architecture expands. For instance, you can designate a distinct service for /api/user/* while still maintaining the standard service for other /api/* routes.

While there may come a time when a commercial product is necessary, for my current and foreseeable requirements, nginx continues to be an exceptional solution.

The critical component of my nginx configuration is outlined below:

server {
    listen 80;

    # To prevent Play from serving assets from its bundled jar, which works but is unnecessary when nginx can handle these files directly.
    location /assets {
        alias /app/live/my-play-app-here/active/public;
    }

    location / {
        proxy_pass http://localhost:9000;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

The emphasis lies on the /assets URI-space. Your setup may differ if your AngularJS app is packaged independently. In my case, the angular app resides within the Play app's /app/assets/javascripts directory. While there are pros and cons to this approach, I find it advantageous to allow nginx to serve static content instead of Play.

This information may not apply to your situation, but for those with everything contained within Play, unpacking the public directory during deployment (such as after executing play dist) is crucial. Here's an excerpt from my deployment script illustrating this process:

unzip lib/$SERVICE_BASE_NAME.$SERVICE_BASE_NAME-$VERSION.jar "public/*"

For your specific needs, consider starting with a configuration similar to the following:

server {
    listen 80;

    location /api {
        proxy_pass http://localhost:9000;
        proxy_set_header X-Real-IP $remote_addr;
    }

    location / {
        alias /app/live/my-angularjs-app-here/active/public;
    }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Develop a custom directive that incorporates ng-model and features its own distinct scope

UPDATE - I have generated a Plunker I am in the process of developing a personalized directive to be utilized for all input fields. Each input will have distinct options based on the logged-in user's requirements (mandatory, concealed, etc), so I bel ...

Encountering an issue with npm installation in the angular-phonecat tutorial: Module 'update-notifier' not found

Having trouble running npm install in the angular-phonecat directory. Could be a problem with my paths, but I'm not certain. Any help would be greatly appreciated. Keep running into Error: Cannot find module 'update-notifier'. If I try to in ...

Discovering the values within a separate JSON object

I have a collection of json objects that include information about different languages and user details. Languages User Details The user details contain a field for languages, which can have multiple values. Below is a sample json: $scope.languages = ...

Creating a div element with a fixed size that remains the same regardless of its content

Check out the code snippet below for displaying an image and title: <div id="eventsCollapsed" class="panel-collapse collapse" ng-controller="videoController"> <div class="panel-body"> <div ...

Setting up NGINX (Engintron) for Node.js on a particular port to switch from HTTPS to HTTP

I am completely new to web server configurations and have been struggling to find a working setup for weeks. Any advice or comments would be greatly appreciated! I currently have a CentOS machine with cPanel (EasyApache running on ports 8080 and 8443) and ...

Encountering a Post Error when Using MongoDB with Node.js

I encountered an issue when attempting to post to the DB, receiving an Error: 'Cannot POST /api/postCampaign' and a 404 (Not Found) error for . My goal is to store data in a MongoDB database. Initially, this code was functioning properly as it co ...

Accessing form objects in Typescript with AngularJS

I am currently working with AngularJS and Typescript. I have encountered an issue while trying to access the form object. Here is the HTML snippet: <form name="myForm" novalidate> <label>First Name</label> <input type="text" ...

Altering the background-color of the navigation bar based on the current

As a newcomer to the Angular framework, my current objective is: To dynamically change the background color of my navbar while scrolling on the homepage, and have it maintain a fixed background color when navigating to other pages. To accomplish this goa ...

AngularJS fetching data with two separate AJAX requests for both HTML and JSON responses

After activating network capture in my web browser to inspect the ajax calls coming out of AngularJS, I noticed that each call seems to result in two outcomes: URL|Protocol|Method|Result|Type|Received|Taken /p/jobs/03512dc8-6f25-49ea-bdff-0028ac2023cb|HTT ...

Show the value in the input text field if the variable is present, or else show the placeholder text

Is there a ternary operator in Angular 1.2.19 for templates that allows displaying a variable as an input value if it exists, otherwise display the placeholder? Something like this: <input type="text "{{ if phoneNumber ? "value='{{phoneNumber}}&a ...

Utilize AngularJS to loop through a list with ng-repeat

Seeking guidance as an Angular newbie. The ng-repeat in question is formatted as: ng-repeat="f in drillDownList['D' + d.merchMetrics.DEPT_NBR + 'CG' + d.merchMetrics.CATG_GRP_NBR + 'C' + d.merchMetrics.DEPT_CATG_NBR] M ...

After configuring Python Selenium with a proxy, the message 'There are no active modal dialogs' is displayed

While using a proxy with my webdriver, an authentication dialog requiring user/pass credentials appears. To address this, I opted to utilize an autoit script for a simpler solution: #Include <File.au3> WinWaitActive("Authentication Required") Send( ...

Utilize the ng-click feature for swiping interactions in Ionic v1

I have a slide page on Ionic V1 that utilizes previous and next buttons. Here is an example: <button id="" class="button button-slide prev no-animation" ng-click="prev()" ng-show="activeIndex > 0" > BACK </button> While the click function ...

What steps do I need to take to build something similar to this using AngularJS?

Struggling with understanding the concepts of AngularJs. How can I create textfields and animations like the ones in this example using AngularJS? I've tried exploring directives, but it's not quite clicking for me. I've attempted to follow ...

The issue of passing a local object from ng-repeat into a directive controller remains unresolved

I'm struggling to pass my local object from ng-repeat to a directive. My goal is to access the data from that object within the controller of the directive. The confusion arises with how the isolated scope and controller scope interact. I can't f ...

Steps to trigger an AngularJS modal window when the URL contains a query string

I am in the process of creating a website and I would like to have a $uiModal open when there is a querystring in the URL. If there is no query string, then the modal should not open. Here is the code snippet: myApp.controller('jobObjectiveController ...

Why is my Angular promise unexpectedly landing in the error callback?

I am facing an issue with my Angular + Typescript client. I have developed a PHP API and need to send a post request to it. Upon receiving the request, the server fills the response body with the correct data (verified through server debugging). However, w ...

Utilizing a variable within an Angular filter: A step-by-step guide

Currently, I am in the process of experimenting with Angular. I am able to retrieve some data from the controller and successfully apply a filter when passing it as a string. However, I encounter issues when attempting to use a variable for the filter inst ...

The AngularJS functionality is failing to execute within the Dockerfile during the npm installation process. The issue

Update: I encountered an issue where I can't name the container client. There seems to be a hidden container that I am unable to delete, causing some inconvenience. I am in the process of setting up a Docker file environment for a MEAN stack applicat ...

Relocating the Asp.Net WebAPI to its own domain apart from the Asp.Net MVC site in preparation for the AngularJS

We currently have an Asp.Net MVC 5.0 project that utilizes AngularJS for the front end. The project includes a WebAPI Controller within the same project and is deployed on the same website. This application was created using the Individual User Account pro ...