Heroku和multer [英] Heroku and multer

查看:72
本文介绍了Heroku和multer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个Webapp,可让您使用multer将文件上传到服务器.当服务器在本地运行时,它的工作方式应该像它应该的那样,但是当我在Heroku上部署它时,似乎遇到了500个内部服务器错误.

I made a webapp that lets you upload a file to the server using multer. It works like it should when the server is run locally, but when I deployed it on Heroku, it seems I ran into a 500 internal server error.

以前有人处理过吗?

有哪些选择?

Webapp在这里: https://dupefinder.herokuapp.com/

Webapp is here: https://dupefinder.herokuapp.com/

Github仓库在这里: https://github.com/ExtDASH/herkodeploy

Github repo is here: https://github.com/ExtDASH/herkodeploy

2018-09-19T19:38:48.310177+00:00 app[web.1]: POST /uploads 500 148 - 181.170 ms
2018-09-19T19:38:48.310830+00:00 app[web.1]: Error: ENOENT: no such file or directory, open 'uploads/csv1537385928295.csv'
2018-09-19T19:38:48.311255+00:00 heroku[router]: at=info method=POST path="/uploads" host=dupefinder.herokuapp.com request_id=ff1aaa34-f36c-49cf-bd4e-4a936fb48a2c fwd="24.52.32.175" dyno=web.1 connect=1ms service=188ms status=500 bytes=404 protocol=https

这是浏览器中的控制台错误:

and here is the console error in the browser:

main.js:146 POST https://dupefinder.herokuapp.com/uploads 500 (Internal Server Error)
reader.onload @ main.js:146
load (async)
readFile @ main.js:131
invoker @ vue.js:2029
Vue.$emit @ vue.js:2538
click @ VBtn.ts:108
invoker @ vue.js:2029
fn._withTask.fn._withTask @ vue.js:1828

我正在使用XMLHttpRequest POST请求:

I'm using an XMLHttpRequest POST request:

readFile: function(){
        const input = document.querySelector('#myFile')
        const reader = new FileReader()
        reader.onload = function() {
            let csvfile = new Blob([reader.result], { type: 'text/csv' })
            app.uploadingFile = true

            const form = new FormData()
            let sendName = input.files[0].name.split(/\W+/g)

            form.append('Ncsv', csvfile, `${sendName[0]}.csv`)
            const xhr = new XMLHttpRequest()
            xhr.open('POST', '/uploads', true)
            xhr.onreadystatechange = function() {
                if(this.readyState == XMLHttpRequest.DONE && this.status == 200) {
                    form.delete('Ncsv')
                }
            }
            xhr.send(form)

        }
        reader.readAsText(input.files[0])

转到我的server.js文件中的app.post路由:

which goes to an app.post route in my server.js file:

const express = require('express')
const connect = require('connect')
const morgan = require('morgan')
const bodyParser = require('body-parser')
const mongoose = require('mongoose')
const fs = require('fs-extra')
const multer = require('multer')
const getRouter = require('./routes/ourNums')
const nFs = require('./fileSchema.js')
const namesRouter = require('./routes/namesRouter.js')
const computeRouter = require('./routes/computeRouter.js')

// const uploadRouter = require('./routes/uploadRouter') unused for now
const filesRouter = require('./routes/filesRouter')
const path = require('path')



const app = express()

var storage = multer.diskStorage({
destination: function (req, file, cb) {
    fs.ensureFile(file)
    .then(() => {
        console.log('done')
     }
    cb(null, __dirname+'/uploads')
},
filename: function (req, file, cb) {
    var newName = file.originalname.split(/\W+/g)
    var fullName = `${newName[0]}${Date.now()}.csv`
    cb(null, fullName)
},
})



var upload = multer({ storage: storage })

app.use(express.json({limit: '50mb'}))
app.use(bodyParser.urlencoded({extended: false}))
app.use(bodyParser.json())
app.use(bodyParser.json({ limit: '50mb' }))
app.use(morgan('tiny')) //watching for changes
// app.use(express.static(`${__dirname}/client/index.html`))

app.post('/uploads', upload.single('Ncsv'), function (req, res, next) {
var fileName = req.file.filename
nFs.create({
    name: fileName
})
.then(data => res.status(200).send())
.catch(e => {
    req.error = e
    console.log(e)
    next()
})
})

之前,我没有使用fs进行任何操作(正如您在此处看到的,fs.ensureFile并没有采取任何措施来修复500错误),我只是将其包括在内,因此我可以尝试一下用它.在本地运行服务器,可以正常工作.我在客户端上单击上传"按钮,该按钮会发送我选择为Blob的任何文件,通过multer运行该文件,然后在/server/uploads/目录中创建文件

before, I didn't use fs for anything (and as you can see here, fs.ensureFile isn't doing anything to fix the 500 error), I just included it to begin with so I could play around with it. Running the server locally, this works. I click my upload button on my client and it sends whatever file i selected as a blob, runs it through multer, and creates the file in a /server/uploads/ directory

我刚刚尝试使用multer.memoryStorage()并得到了相同的500个内部服务器错误.

I just tried using multer.memoryStorage() and got the same 500 internal server error.

推荐答案

我不确定为什么不保存您的上载;您应该可以暂时保存它们.

I'm not sure why your uploads aren't being saved; you should be able to save them temporarily.

但这不会长期有效. Heroku的文件系统是短暂的:您下次进行的任何更改都会丢失dyno重新启动,频繁发生(每天至少一次).

But this won't work long-term. Heroku's filesystem is ephemeral: any changes you make will be lost the next time your dyno restarts, which happens frequently (at least once per day).

Heroku 建议将上传内容存储在Amazon S3之类上.这是专门用于Node.js的指南.

Heroku recommends storing uploads on something like Amazon S3. Here's a guide for doing it specifically with Node.js.

将文件存储在S3上之后,您应该能够使用适当的库或通过HTTP(取决于存储桶的配置方式)来检索文件.

Once you've stored your files on S3 you should be able to retrieve them using an appropriate library or possibly over HTTP, depending on how you've configured your bucket.

如果您仍然希望使用multer,请查看 multer-s3

If you still wish to use multer, check out multer-s3.

这篇关于Heroku和multer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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