1

I have a question about how to integrate Angular with NodeJS and Spring Boot. I have developed an application with Angular as UI and Java/Spring Boot as backend REST API. The problem is that SEOs in a SPA do not work well. For that reason, some developers created Angular Universal to render Angular applications on the server side. Unfortunately, Angular Universal works only with ASP.NET or NodeJS.

Some attempt was made to make it work with Java as well, (see: https://github.com/swaechter/angularj-universal), but the repo has been archived/abandoned.

If I understand it correctly, the approach would be to run a NodeJS server that is in charge of rendering the Angular UI server side only, while the java service would be in charge of exposing the REST API, but since I have no experience with NodeJS or Express.js, I am not sure if that's the right way to proceed and what implications should I be care of. I couldn't find any guide online.

But my question is, should the code in angular make requests to nodeJS and then nodeJS forward them to the java service, or should angular contact directly the java service to get the data, and only NodeJS to do the SSR?

Thanks

1
  • You sould only use nodejs for performing SSR your application. You can keep using Java for the API Commented Aug 2, 2021 at 12:25

1 Answer 1

0

If I understood you correctly, what you are trying to do is called prerendering an Angular application, AFAIK Angular Universal uses NodeJS to do that.

An Angular application is usually compiled to vanilla JS that the browser can understand, which means that in build time you could compile the parts of your application that are static (aka "known at build time"), this is done by NodeJS. This has a problem, if your static parts use browser facilities (e.g. localStorage) or call backend services you have to wrap them with:

if (isBrowser) {
  localStorage.set('example', 5);
  fetch('example.com/service');
}

In your pipeline you have to build the project and move the generated JS, HTML and CSS to be served directly through your application server or proxy if you have (e.g. nginx).

Here I have an example of a real project that you could take a look that do prerendering with Angular js and Spring Boot. In this project the angular application is served through Spring Boot. https://github.com/afdezcl/kemenu-web

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. I looked at your project. Yours is a SPA I think, you server index.html through spring boot, but you are not doing SSR. I'd like to do SSR with Angular Universal (AU) and Spring Boot but AU doesn't quite support a java backend.
Prerendering is not full SSR but gives you the opportunity of improve the SEO a lot. You can always create your frontend with thymeleaf (or whatever) and add angular as a JS dependency like old school JQuery.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.