针对Google App Engine灵活生产环境中公共目录的Node.js应用404错误

时间:2016-12-24 06:23:26

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

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

对于一些人(对我来说不那么明显)的原因,我最近在Google Flexible生产环境中努力工作。在我的本地开发环境中,一切都很好。

为了缩小问题范围,我在此处创建了一个非常基本的测试应用程序:

    '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 app部署 到谷歌制作我得到404(也在谷歌日志)。在当地的发展环境中,一切都很好。

以下是生产环境的输出:

Production

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

Local Development

正如您在Google prod上看到的那样,HB.png的图像链接已损坏。在Google日志中会显示404。

有没有人有线索?提前谢谢!

1 个答案:

答案 0 :(得分:0)

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