发布路线/ upload的方法正常工作。但是,添加路由/ uploadfortextformat后,/ upload方法出现冲突错误。
app.use(express.static('media'));
const cors = require('cors');
const multer = require('multer');
//save image from text format
app.use(cors({credentials: true, origin: 'http://localhost:3000'}));
var fileUpload = require('express-fileupload');
var mime = require('mime');
app.use(fileUpload({}));
app.post('/uploadfortextformat', function (req, res) {
console.log('textformat called');
var folderName = 'media/images';
var path = require('path');
if (!req.files) {
return res.status(400).send('No files were uploaded.');
}
console.log(req.files.file.name);
console.log(req.files.file.data.byteLength);
var sampleFile = req.files.file;
sampleFile.mv(path.join(folderName, sampleFile.name), function (err) {
var temp = path.join(folderName, sampleFile.name);
mime.lookup(path.join(folderName, sampleFile.name)); // =>
'text/plain'
if (err) {
return res.status(500).send(err);
}
res.send({ 'location': 'http://localhost:4000/images/'+sampleFile.name
});
});
});
//media upload
var storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'media/images')
},
filename: (req, file, cb) => {
cb(null, file.originalname)
}
});
const upload = multer({ storage })
app.post('/upload', upload.single('image'), (req, res) => {
console.log('upload called');
if (req.file)
res.json({
imageUrl: `images/${req.file.filename}`
, file_size : req.file});
else
res.status("409").json("No Files to Upload.");
});
以上两个功能都是将图像文件上传到同一文件夹。我不知道这两种方法之间的冲突是什么原因。 如何摆脱409错误?