这些代码在本地环境中运行良好,但是当我推向openshift时,在Openshift中不起作用,我还发现可以使用“ process.env.OPENSHIFT_DATA_DIR”获取图像的上载路径,但是我无法获得所需的结果,它是未定义的,并且这是我的代码,在这种情况下我是完全陌生的,因此请帮助演示一个示例,以便我轻松理解该过程。谢谢
// set storage engine
const storage = multer.diskStorage({
destination:'./public/uploads',
filename: function(req,file,cb){
cb(null,file.fieldname + '-' +Date.now() +
path.extname(file.originalname));
}
});
// Init upload
const upload = multer({
storage: storage
}).single('myImage');
//////// post route for upload images
app.post('/adsupload',(req,res)=>{
console.log("detail before saving"+req.file)
upload(req, res, (err) =>{
if(err){
res.render('imageupload',{
msg: err
});
} else if(req.file== undefined){
res.render('imageupload',{
msg:'Error: No file selected'
});
}else{
console.log("detail before saving"+req.file)
var dirName = process.env.OPENSHIFT_DATA_DIR;
console.log('dirname'+dirName)
console.log('dirname2'+OPENSHIFT_DATA_DIR);// these are undefined
var ads = new Ads({
ads_name: req.file.filename,
ads_path: path.join(__dirname+'./public/uploads/'+req.file.filename)
})
ads.save().then((doc)=>{
// res.send(doc)
console.log('image saved in database')
},(e)=>{
// req.status(400).send(e)
console.log('error while saving image in database'+err)
});
console.log('getting filename'+ req.file.filename);
res.render('imageupload',{
msg: 'File Uploaded',
file: 'uploads/'+req.file.filename
});
}
})
})
答案 0 :(得分:1)
OpenShift 3不像OpenShift 2那样为您提供持久数据目录。您将需要声明一个永久卷并将其装入某个目录中的应用程序。然后,您应该在应用程序的部署配置中设置OPENSHIFT_DATA_DIR
,以告诉应用程序将数据目录安装在何处。