Server side React with Java 8 Nashorn

A Java 8 process with an embedded rest-api and web application, where the web application user interface is constructed using the React JavaScript components via the Nashorn engine with access to Java services.

Coming from the Enterprise Java stack arena and trying to learn all the facets of the NodeJS eco-system, not to mention JavaScript language itself, is no easy task when you only have an hour a day at best.

Having explored various tutorials across the JavaScript landscape I settled on React for some deeper UI work. Problem was, everything else around it felt too distracting. The node setup, the frameworks, the libraries, client-v-server side rendering, and ‘what the Flux is all that about’. Meanwhile I’d come across a clean and simple library called SparkJava (not to be confused with Spark from the BigData ego-system). SparkJava is built for Java 8 and nicely aligned to the typical NodeJS construct of defining functional methods for handling a path/request/response.

SparkJava – A tiny Sinatra inspired framework for creating web applications in Java 8 with minimal effort

So I began to play around with the possibility of creating a light-weight Java 8 micro-service but leveraging the richness of React to perform server-side rendering.  To what end? I wasn’t even sure it would work.  But it did, surprisingly well.

The Bootstrap-Admin-Theme using Java 8 with Nashorn and React for rendering

Head on over to my GitHub repository for a closer look and more details.  There really isn’t much code once you understand the basic pieces that have been pulled together.

Java 8 process with Nashorn and React server-side rendering


When might you want to use this?

  • If you have an Enterprise Java stack and concerns about adding NodeJS
  • If most of your development team is Java skilled, with limited JavaScript skills
  • When you want to serve fully server-side generated SEO friendly pages
  • When most of the data rendered on the page does not need to be dynamic

Final thoughts…

It would be interesting to come back and see if this project could make use of the isomorphic capabilities of ReactJS, to provide dynamic updates from the client-side too.  But for now, I think this fills a niche use-case for serving up server-side pages of data extracted right out of a Java process without the need for another layer of middleware.

It's only fair to share...Tweet about this on Twitter
Share on LinkedIn
Email this to someone

Leave a Reply

Your email address will not be published. Required fields are marked *