Implementing Angular Universal with existing Express Server

Viewed 186

I have an Express server that serves Angular app as a static content, but it also does a lot of other things. Now, I want to add Server-side Rendering with Angular Universal, but that creates new Express server that will serve Angular app. How can we serve Angular Universal with already existing Express app? This should be common feature since a lot of projects didn't implement Angular Universal from the start, and has to add it in already existing server. It is strange that official Angular docs does not say anything about it.

I want to mention that I implemented Express server with JavaScript, and not TypeScript.

1 Answers

Maybe try something like this, from your existing express application

const server = require(`dist/server/main.js`);//file generated by angular universal
const universalApp = server.app();

const server = <your existing express server>;
server.use('/frontappUrl', universalApp);

server.listen(<port>,...);
Related