Sagiruddin Mondal
Sagiruddin Mondal

Reputation: 5787

Best way to have play framework and angular working together

I have been working with play framework and angular together for more than two years. My question is I need to be sure about the best between these two ...

  1. Play and angular can be merged in a single project and play routing can directly feed angular route. This way the view part of the play MVC is directly replacing the angular architecture. The cons I know already is it is not manageable when you are planning for another media (maybe like android) to get API feed from play or you may want to handshake for authorization from other media.

  2. The second way is cleaner when you are just using a separate angular app (like yeoman or from scratch) where play will feed as an API service platform and angular will run in an independent server. Here I am not quite sure about the burden to deploy angular if I am not using any cloud. or similar things.

Now my question is which way is more preferred and modern and logical ?

Upvotes: 1

Views: 544

Answers (1)

LiorH
LiorH

Reputation: 18824

You're correct with your analysis, the first approach loses the advance routing angular can offer, while the second approach is an overkill for small apps (running two servers).

We've used a combination of the two methods, run angular & play in the same folder/repository. The trick is to have angular "build" it's artifacts into a folder later loaded by play as a public folder. Then separate api routes from the static ones, so for example, all /public/* urls go to the public folder. while the api/* routed to standard play controllers.

Adding external folder to play public folders can be done with the following line in build.sbt

unmanagedResourceDirectories in Assets += baseDirectory.value / "client/build"

See this answer for details.

So eventually the project looks something like this:

  • build.sbt
  • package.json
  • public
  • app //play code
  • client //client code
    • build // client "compiled" output, loaded as public folder to play

of course there can be variants on this solution, e.g instead of adding client/build as unmanaged resource folder to play, copy its content at build time to the standard play public folder.

Upvotes: 1

Related Questions