【问题标题】:Node.js app 404 errors for public directory in Google App Engine Flexible production environmentGoogle App Engine 柔性生产环境中公共目录的 Node.js 应用程序 404 错误
【发布时间】:2017-05-09 17:13:13
【问题描述】:

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

由于某些(对我来说不是很明显)的原因,我最近很难在 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(也在谷歌日志中)。在本地开发环境中一切都很好。

这是生产环境的输出:

Production

这是本地开发环境的输出:

Local Development

正如您在 Google prod 上看到的那样,指向 HB.png 的图片链接已损坏。在 Google 日志中出现 404。

有人知道吗?提前致谢!

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-16
      • 2019-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多