我在multer
中使用node.js
将文件(来自基于Angular的客户端项目)上传到我的Heroku服务器。一切正常,但当Heroku服务器重新启动或关闭时,所有上传的文件都会消失,URL hits
会返回' Not Found
'。
这是我上传文件的代码:
var express = require('express');
var router = express.Router();
var mysql = require('mysql');
var multer = require('multer');
var pool = require('./dbconnection');
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, '/app/public/images/postImages/')
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now()+'.jpg')
}
})
var upload = multer({ storage: storage })
router.post('/postImages', upload.single('image'), function(req, res) {
var userId = req.body.userId;
var postTitle = req.body.postTitle;
var postDesc = req.body.postDesc;
var postLat = req.body.postLat;
var postLng = req.body.postLng;
var postMediaType = req.body.postMediaType;
var postMediaFileName = req.file.filename;
var postMediaFilePath = req.file.destination;
var postMediaFileURL = req.file.path;
postMediaFileURL = postMediaFileURL.substring(postMediaFileURL.indexOf('images/'), postMediaFileURL.length);
var inserts = [postTitle, postDesc, postLat, postLng, postMediaType, postMediaFileName, postMediaFilePath, postMediaFileURL, userId];
var sql = "INSERT INTO posts (postTitle, postDesc, postLat, postLng, postMediaType, postMediaFileName, postMediaFilePath, postMediaFileURL, userId) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
sql = mysql.format(sql, inserts);
console.log(sql);
pool.getConnection(function(err, connection) {
connection.query(sql, function(error, results) {
connection.release();
res.json(results);
if(error) throw error;
});
});
});
module.exports = router;
告诉我我错过了什么或我做错了什么。
答案 0 :(得分:1)
答案在你的问题中,这是heroku。应用程序重新启动后,只有部署的文件将保留在服务器上。
我建议使用远程存储,我建议将'cloudinary'作为媒体存储的免费解决方案。
但是如果你想坚持使用heroku作为存储,总会有升级选项。