简体   繁体   中英

Node.js app 404 errors for public directory in Google App Engine Flexible production environment

I've been working with the Node.js Google App Engine for some months and have always successfully used the express.static solution to access static files in the public folder when i deployed my node.js app.

For some (to me not so obvious) reason I struggle to get this working lately in the Google Flexible production environment. On my local development environment everything is fine.

In order to narrow down the problem I created a very basic test app listed here:

    '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/>\
 ')
})

I tried various settings of the express.static settings (see those commented out). However each time after deploying using gcloud app deploy to Google production I get 404 (also in the google logs). On local development environment everything is fine.

Here's the output of the production environment:

Production

And here's the output of the local development environment:

Local Development

As you can see on Google prod the image links to HB.png are broken. In the Google logs a 404 appears.

Does anyone have a clue ? Thanks in advance !

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM