Google App Engine 灵活生产环境中公共(public)目录的 Node.js 应用程序 404 错误

标签 node.js google-app-engine express http-status-code-404 static-files

我使用 Node.js Google App Engine 已经有几个月了,在部署 Node.js 应用程序时,我总是成功地使用express.static 解决方案来访问公共(public)文件夹中的静态文件。

由于某些(对我来说不是那么明显)原因,我最近很难在 Google 灵活的生产环境中使其正常工作。在我本地的开发环境中一切都很好。

为了缩小问题范围,我创建了一个非常基本的测试应用程序,如下所示:

    'use strict'

const express = require('express')
const app     = express()
const path    = require('path')
const os      = require('os')
const PORT    = process.env.PORT || 8080
const ENV     = process.env.NODE_ENV

//app.use(express.static('public'))
//app.use(express.static(path.resolve(__dirname, 'public')))
app.use(express.static(path.join(__dirname, 'public')))

app.listen(PORT, () => {
 console.log(`SYSTEM: App listening on port ${PORT}`)
 console.log(`SYSTEM: Press Ctrl+C to quit.`)
})

app.get('/', (req,res) => {
 res.status(200).send('\
  <h1>TEST app.use(express.static("public")) in Google Cloud Flexibel App     Engine environment </h1>\
  <hr/>\
  <h4>YAML settings: runtime: nodejs env: flex</h4>\
  <h4>HOST         : '+`${os.hostname()}`+'</h4>\
  <h4>PORT         : '+`${PORT}`+'</h4>\
  <h4>__dirname    : '+`${__dirname}`+'</h4>\
  <h4>mountpath    : '+`${app.mountpath}`+'</h4>\
  <h4>env          : '+`${ENV}`+'</h4>\
  <h4>path resolved: '+`${path.resolve(__dirname, 'public')}`+'</h4>\
  <h4>path joined  : '+`${path.join(__dirname, 'public')}`+'</h4>\
  <hr/>\
  <h2>If you see me <img src="./HB.png"> you can access "./HB.png" in the "public" directory.</h2>\
  <h2>If you see me <img src="/HB.png">  you can access "/HB.png"  in the "public" directory.</h2>\
  <h2>If you see me <img src="HB.png">   you can access "HB.png"   in the "public" directory.</h2>\
  <hr/>\
 ')
})

我尝试了express.static 设置的各种设置(请参阅注释掉的设置)。但是每次部署后使用 gcloud 应用部署 到谷歌生产我得到404(也在谷歌日志中)。在本地开发环境上一切都很好。

有人知道吗?提前致谢!

最佳答案

奇怪,我通过重新安装Google Cloud SDK解决了这个问题。

关于Google App Engine 灵活生产环境中公共(public)目录的 Node.js 应用程序 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41310899/

相关文章:

javascript - 从 Promise.all 中获取 null 值

node.js - 如何将 Handlebars 文件复制到 dist 文件夹

java - Google Drive API 发送推送通知时的 HTTP 302

java - 在 Eclipse 中升级到新的云工具插件后,本地服务器显示 "stopping, synchronized."

angularjs - 如何使用反向代理处理 Express 应用程序中的重定向

session - node.js + express.js : session handling with mongodb/mongoose

node.js - 运行时加载 Typescript 动态模块

JavaScript 查找 : update value of object in array if object key exists in another object array similar to the v-lookup in excel

python - 如何防止 AppEngine 内存缓存刷新

node.js - Express 和 AngularJS - 尝试打开主页时网页卡住