0

I'm trying to host an Angular app in Google cloud app engine. Using express to serve the content and static should come from the google CDN.

Dist is built and delivered to the CDN. CDN bucket has been shared as well.

I got it all working as a basic express app serving static content from the local ./dist folder. How to configure this to work against CDN?

'use strict';
var express = require('express');
var app = express();
app.use(express.static('dist'));
app.get('/', function (req, res) {
  res.render('index.html');
});

var server = app.listen(process.env.PORT || '8080', function () {
  console.log('App listening on port %s', server.address().port);
  console.log('Press Ctrl+C to quit.');
});

Appreciate any pointers.

Thanks !!

Sahas
  • 3,046
  • 6
  • 32
  • 53

1 Answers1

2

You don't need to use express static at all if you are serving your static content from a cdn. Just point to the assets using the cdn urls.

A pretty typical thing to do might be to have a helper that loads the right cdn url in your templates. Something like this:

<head>
  <link href="{{cdn '/css/app.css'}}">
</head>

In dev mode, that just resolves to http://localhost:3000/css/app.css but in production it might be https://cdn-something.com/2.10.2...

You could conditionally load the express static middleware when in the development environment. Or you could serve the static assets locally using something like apache, nginx, or even another node process on a different port than your application.

Robert Moskal
  • 21,737
  • 8
  • 62
  • 86