【发布时间】:2021-01-10 10:50:37
【问题描述】:
我有一个简单的 html 文件,其中包含这样的 css:
<link rel="stylesheet" href="./css/style.css" />
我把这个html文件和css文件上传到google cloud storage,并设置权限为public。
在我的应用程序中,用 node js 编写,我想在用户访问我的根页面时提供这个 html 文件。
在我的应用程序中,我有以下代码:
public async getPublicFile(opts: IGetFileOpts): Promise<File> {
const bucket = this.storage.bucket(opts.bucket);
return bucket.file(path.join(opts.type, opts.fileName));
}
@Get()
public async serveFile(@Res() response: Response) {
const file = await this.storageService.getPublicFile({
organization: organization,
fileName: 'index.html',
type: 'resources',
});
file.createReadStream().pipe(response);
}
这按预期工作。它将从存储桶中服务index.html。但是,由于这个 html 文件有相对于 css 文件的链接,所以它不会加载 css 文件,因为它找不到它。
我该如何解决这个问题,以便也可以提供 css 文件?目前我在本地计算机上运行它,但它将部署到 Google Compute Engine。
我找到了这个 AppEngine 的链接https://cloud.google.com/appengine/docs/standard/go/serving-static-files
在 AppEngine 中,我可以像这样定义一些处理程序:
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: /static
static_dir: public
- url: /.*
secure: always
redirect_http_response_code: 301
script: auto
但据我了解,这在本地机器上不起作用。
另外,电子商务公司如何解决这个问题?例如,每个商店都可以有不同的主题,可以定制。所以我知道每个租户都有自己的存储桶,并且在租户存储桶中,这个可自定义的主题保存正确吗?所以我如何假设应该像我一样有类似的问题。如何应对这种情况以及如何处理?
【问题讨论】:
-
实际上,如果您更改为 Node.js 示例共享的文档链接,则有一整节内容是关于如何为本地开发提供文件,其中一个
.css示例使用express.static来申请它,这里是特定部分的link,因此生产和本地开发都可以。当然,这是针对 AppEngine,而不是 Compute Engine,但它是一种可能的替代方案,这样就足够了吗? -
如果您的 css 目录可通过 http 或 https 访问,您可以在服务器端添加一个新的
<base>标记,同时返回响应以更改页面的基本 URL。这样 html 由应用程序引擎提供服务,静态资产从云存储中提取。阅读更多关于base标签here。
标签: node.js google-cloud-platform google-cloud-storage e-commerce