question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature request for a non Node.js depdendant server side platform server

See original GitHub issue

Background

Hey there, I would like to start a discussion and ask for a feature request. I tried to keep this feature request as short & understandable as possible, but somehow failed miserably 😉

I am a Java backend developer and really interested in Angular. Last year, I wrote a small Java library that allows a developer to server side render an Angular application from a Java stack. For this, the library uses Java language bindings for Node.js/V8 (https://github.com/eclipsesource/J2V8). You can find my project here: https://github.com/swaechter/angularj-universal

I would like to spend more time on my library and add more features and make it production ready. The problem is, that I am experiencing several technical limitations that I personally can’t solve or where there is no good alternative. So I am reaching out to you for input/feedback and a possible solution.

Overview

To give you a short overview how my library works, just take a look at this diagram:

diagram1

You create a normal Angular application via Angular CLI and then generate the universal server. In addition, some glue code is required, so the J2V8 Node.js instance can load the server bundle and can call a method to render page requests via renderModuleFactory. The result is then passed back to the Java system and sent back to the client.

Important node: The J2V8 JavaScript engine allows us to dynamically register JavaScript functions like receiveResponse that can be implemented in Java and called from JavaScript. You can find the glue code in this file here: https://github.com/swaechter/angularj-universal/blob/master/angularj-universal-application/src/main/angular/library/renderadapter.ts

You can compare this render adapter to a express.js view engine - just a lot simpler (I guess).

Problems

This solutions works great for small applications with one module and routing. But there exist several larger problems:

Problem 1: Angular requires Node.js and zone.js/dist/zone-node for rendering

The J2V8 JavaScript engine is the only solution that provides a.) modern module loading and b.) an implemented Node.js API. There exist no other library that provide the same functionality in this quality. The problem is, that the project isn’t that actively maintained and the last public releases for Windows/OX X were made in September 2016. I really like the project, but the future is unsure and I would like to have other non-Node.js requiring solutions (Keep in mind: All Java JavaScript engines can dynamically implement functions in Java, so we could re-implement required Node.js parts).

There exist several non-Node.js alternatives, they just don’t implement the Node.js API:

  • Nashorn: Great, out of the box, but no implementation of the Node.js API
  • RingoJS: Great, but no [full] implementation of the Node.js API
  • Rhino: Dead, superseded by Nashorn (But still usable)

So as you can see, J2V8 is a usable solution, but it’s the only available one and it would be good to have alternatives. I am in favor of Nashorn, a JavaScript engine that is provided by the Java runtime out of the box.

Problem 2: Many rendering tasks are done by Node.js or it’s underlaying subsystem

The authentication and session/cookie management is done by the Java wevserver/webframework (For example a session based basic authentication or a stateless JWT token that is stored via a httpOnly secured cookie). This works great, but potential HttpClient requests like /api/addresses during server side rendering are made by Node.js or to be more precise it’s underlying system - and they are not aware of these session/cookies. Hence, the server side rendered request is unable to fetch the data while the client side executed request is able to.

So the question is: How does Node.js know the session/cookie/authentication?

One solution (and in my opinion more a dirty hack) would be to pass down all session/cookie/Http header information and pass them to the HttpClient request. This would work, but violates the protection of httpOnly cookie values. This wouldn’t pass a commercial security review + what happens in the case of a potential XSS during server side rendering?

A more sophisticated and cleaner solution would be to route back the /api/addresses request to the Java environment, where I can securly access all session/cookie information of the user. I then make a manual Java request with these information and pass the result back to the Node.js instance. The Node.js instance will never know anything about httpOnly cookies (We can pass down the available cookies of course).

diagram2

Proposal

In short: Make it possible to replace the Node.js based functionality in ServerModule (Like Http handling) with a custom, non Node.js dependent solution. This has to happen at compile time, so the relocatable server bundle doesn’t require any Node.js module. This also means, that some could (?) use a client-side zone.js.

I read the source code of the ServerModule and thought about ripping it apart, re-implementing some parts in Java and provide a new one (For example JavaServerModule). But I guess this approach is really time consuming and the solution has to keep up with the upstream version.

Please keep in mind that I am not that familiar with the Angular internals and there already might be a solution for that.

I also know that this feature request isn’t asking for some specific features, but is more a starting point for a discussion. Please also take a look at my comments in https://github.com/angular/universal/issues/280

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:14
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
swaechtercommented, Mar 6, 2018

Hey @vikerman any updates on this? Can you share some insight/a roadmap and/or can I help you somehow? It would really be a shame to not see such a feature in Angular

PS: I have no idea who reposted my comment :S

2reactions
alexeaglecommented, Feb 26, 2018

We have discussed this possibility, @vikerman is working on it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why the Hell Would I Use Node.js? A Case-by-case Tutorial
On the server side, a simple Express.js application implements: A GET / request handler ... Node.js is not the best platform to handle...
Read more >
A Server-Side JavaScript Security Architecture for ... - Hindawi
In order to support the secure integration of libraries, we developed NODESENTRY, the first security architecture for server-side JavaScript.
Read more >
Express/Node introduction - Learn web development | MDN
A general understanding of server-side website programming, and in particular the mechanics of client-server interactions in websites.
Read more >
How Many Requests Can a Real-World Node.js Server-Side ...
js world, I was wondering know how many requests can actually handle my Node.js application in production (as a real-world app). Or how...
Read more >
Errors | Node.js v19.3.0 Documentation
const cause = new Error('The remote HTTP server responded with a 500 ... Used when a feature that is not available to the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found