一段时间后,heroku上的node.js不断丢失图像 [英] node.js on heroku keeps losing image after a while

查看:71
本文介绍了一段时间后,heroku上的node.js不断丢失图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间后,我的Node.js应用程序不断丢失静态图像(.jpg,.png ...).它在我本地的win10桌面上甚至在heroku上都不会丢失任何图像,我的webpack bundle.js是从相同的静态路由(/pub或/dist)提供的,它们工作得很好.某种程度上,只有静态图片,当我第一次上传时,它们会在最初的几分钟内正确投放,过一会儿,它就会消失.我正在使用express.static进行静态路由声明,并使用multer进行文件上传.用于测试的文件都是小写.jpg(因为我听说heroku任意更改了所有大写扩展名),所以我不知道是什么原因引起的.

my Node.js app keeps losing static images(.jpg, .png...) after a while. It doesn't lose any images on my local win10 desktop and even on heroku, my webpack bundle.js is served from the same static route(/pub or /dist) and they work just fine. somehow only the static images, they are served alright for first few minutes when I first uploaded then after a while, it disappears. I am using express.static for static route declaration and multer for file upload. The files used for test were all lowercase .jpg(since I've heard heroku arbitrarily changes all uppercase extensions) so I don't know what's causing the problem.

服务器代码:

const storage = multer.diskStorage({
  destination: (req,file,cb)=>{
    cb(null,'pub/')
  },
  filename: (req,file,cb)=>{
    cb(null,Date.now() + file.originalname)
  }
})
const upload = multer({storage:storage})

//access to static files
app.use('/pub', express.static(pubDir))
app.use('/dist', express.static(dstDir))

app.post('/modwimg',upload.any(),(req,res,next)=>{
//here I connect filename from files array to db
})

然后,如果有客户端请求,服务器将从数据库中获取文件名,并在其前面放置"/pub/".它在我的本地计算机和heroku上都可以正常工作.只是heroku上的图像过了一会儿就消失了.

then if there's a client request, the server fetches filename from the db and put '/pub/' in front of it. It works just fine on both my local machine and heroku. it's only that images on heroku disappear after a while.

推荐答案

heroku文件系统是瞬态的.如果要允许用户将文件上传到您的应用程序,则需要使用外部存储(例如S3),数据库Blob或托管服务(例如cloudinary).有关更多信息,请参见此线程: https://www.reddit.com/r/rails/comments/2k9sq4/heroku_any_files_you_upload_will_not_be_saved/

The heroku file system is transient. If you want to allow users to upload files to your app, you'll need to use external storage like S3, database blobs, or a hosted service like cloudinary. See this thread for more information: https://www.reddit.com/r/rails/comments/2k9sq4/heroku_any_files_you_upload_will_not_be_saved/

这篇关于一段时间后,heroku上的node.js不断丢失图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆