Using Angular 2 with Express

It turned so that I decided to use Angular 2 for a frontend part of my fullstack projects. Not without difficulties I finally put all those TypeScript, decorators and components stuff to work. The real problem appeared when time came to deploy it and integrate with Express. Initially I intended to bundle all the app components and dependencies into one js file, which I could link to my index.html, but I failed to find a suitable way to do it. So I just made public the app folder along with node_modules (the latter was given ‘/scripts’ alias in Express routing).

So, 1) maybe someone having troubles with using Angular 2 with express may find my solution helpful, and
2) anyone who knows proper ways to deploy Angular 2 apps is welcome to share their knowledge.

Dealing with dependencies can be a challenge. I suggest that you don’t bundle your code in with the dependencies, but instead bundle your code by itself, then either

  1. Bundle each of your dependencies separately and serve those, or
  2. Edit your index.html to reference your dependencies via CDNs.

I would prefer the second option, myself, and there should be a way to automate this in whichever task runner you’re using. I’ve never used Angular 2, though, so maybe there’s some wrinkle here I’m not accounting for.

1 Like