【问题标题】:how to configure express.static to pull static content from google cloud CDN?如何配置 express.static 从谷歌云 CDN 中提取静态内容?
【发布时间】:2017-01-17 01:27:49
【问题描述】:

我正在尝试在 Google 云应用引擎中托管一个 Angular 应用。使用 express 来提供内容和静态应该来自 google CDN。

Dist 被构建并交付给 CDN。 CDN 存储桶也已共享。

我把它作为一个基本的快速应用程序从本地 ./dist 文件夹中提供静态内容。如何配置它以对抗 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.');
});

感谢任何指针。

谢谢!!

【问题讨论】:

    标签: angularjs google-app-engine express google-cdn


    【解决方案1】:

    如果您从 CDN 提供静态内容,则根本不需要使用 express static。只需使用 cdn url 指向资产。

    一个非常典型的做法可能是让一个助手在您的模板中加载正确的 cdn url。像这样的:

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

    在开发模式下,它只解析为http://localhost:3000/css/app.css,但在生产中它可能是https://cdn-something.com/2.10.2...

    在开发环境中,您可以有条件地加载 express 静态中间件。或者,您可以使用 apache、nginx 或什至在与您的应用程序不同的端口上的另一个节点进程在本地提供静态资产。

    【讨论】:

      猜你喜欢
      • 2020-12-31
      • 2020-09-10
      • 1970-01-01
      • 1970-01-01
      • 2021-09-18
      • 2018-12-11
      • 1970-01-01
      • 2017-10-18
      • 1970-01-01
      相关资源
      最近更新 更多