![](/img/trans.png)
[英]Specifying Node.js version for Google Cloud App Engine Flexible
[英]Node.js app 404 errors for public directory in Google App Engine Flexible production environment
我已经使用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部署到Google产品后,我都会得到404(也在google日志中)。 在当地的发展环境一切都很好。
这是生产环境的输出:
这是本地开发环境的输出:
正如您在Google prod上看到的那样,HB.png的图像链接已损坏。 在Google日志中会显示404。
有人有线索吗 ? 提前致谢 !
奇怪,我通过重新安装Google Cloud SDK解决了这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.