Multer req.file 未定义 - 使用 Node/Express 将图像上传到 s3
Multer req.file is undefined - using with Node/Express for image upload to s3
我知道这个问题已经被问了很多,我已经尝试了几乎所有的答案来试图得到这个 运行 但我一直对 req.file.
未定义
代码如下:
services.imageUpload.js
const aws = require("aws-sdk");
const multer = require("multer");
const multerS3 = require("multer-s3");
aws.config.update({
secretAccessKey: "XXXXXXXXXXXXX",
accessKeyId: "XXXXXXXXXXXX",
region: "XXXXXXX",
});
const s3 = new aws.S3();
const upload = multer({
storage: multerS3({
s3: s3,
bucket: "XXXXXXXXX",
metadata: function (req, file, cb) {
cb(null, { fieldName: "TESTING_META_DATA" });
},
key: function (req, file, cb) {
cb(null, req.s3key);
},
}),
});
module.exports = upload;
routes.imageUpload.js
const express = require("express");
const router = express.Router();
const upload = require("../services/imageUpload");
const singleUpload = upload.single("image");
router.post("/", function (req, res) {
singleUpload(req, res, function (err) {
return res.json({ request: req.file.location });
});
});
module.exports = router;
index.js
const express = require("express");
const app = express();
const cors = require("cors");
const imageUpload = require("./routes/imageUpload");
//Middleware
app.use(cors());
app.use(express.json());
//Image upload
app.use("/upload-image", imageUpload);
app.listen(5000, () => {
console.log("server is running on port 5000");
});
非常感谢您的帮助!相当令人沮丧。目前正在使用邮递员测试 API 并发送一个 POST 请求作为表单数据,键为“image”,值为上传的图像文件。
在你的index.js
你可以带
const upload = require("../services/imageUpload");
在最上面,然后在这里初始化multer中间件
//Middleware
app.use(cors());
app.use(express.json());
app.use(upload.single("image"))
然后在routes.imageUpload.js
你可以这样读取上传的文件
router.post("/", function (req, res) {
let fileDetails = req.files
console.log(fileDetails )
.
.
.
});
或者如果您只需要 routes.imageUpload.js
中的上传器,您可以像这样使用中间件,
const express = require("express");
const router = express.Router();
const upload = require("../services/imageUpload");
const singleUpload = upload.single("image");
router.post("/",[singleUpload],function (req, res) {
let fileDetails = req.files
console.log(fileDetails )
.
.
.
});
我知道这个问题已经被问了很多,我已经尝试了几乎所有的答案来试图得到这个 运行 但我一直对 req.file.
未定义代码如下:
services.imageUpload.js
const aws = require("aws-sdk");
const multer = require("multer");
const multerS3 = require("multer-s3");
aws.config.update({
secretAccessKey: "XXXXXXXXXXXXX",
accessKeyId: "XXXXXXXXXXXX",
region: "XXXXXXX",
});
const s3 = new aws.S3();
const upload = multer({
storage: multerS3({
s3: s3,
bucket: "XXXXXXXXX",
metadata: function (req, file, cb) {
cb(null, { fieldName: "TESTING_META_DATA" });
},
key: function (req, file, cb) {
cb(null, req.s3key);
},
}),
});
module.exports = upload;
routes.imageUpload.js
const express = require("express");
const router = express.Router();
const upload = require("../services/imageUpload");
const singleUpload = upload.single("image");
router.post("/", function (req, res) {
singleUpload(req, res, function (err) {
return res.json({ request: req.file.location });
});
});
module.exports = router;
index.js
const express = require("express");
const app = express();
const cors = require("cors");
const imageUpload = require("./routes/imageUpload");
//Middleware
app.use(cors());
app.use(express.json());
//Image upload
app.use("/upload-image", imageUpload);
app.listen(5000, () => {
console.log("server is running on port 5000");
});
非常感谢您的帮助!相当令人沮丧。目前正在使用邮递员测试 API 并发送一个 POST 请求作为表单数据,键为“image”,值为上传的图像文件。
在你的index.js
你可以带
const upload = require("../services/imageUpload");
在最上面,然后在这里初始化multer中间件
//Middleware
app.use(cors());
app.use(express.json());
app.use(upload.single("image"))
然后在routes.imageUpload.js
你可以这样读取上传的文件
router.post("/", function (req, res) {
let fileDetails = req.files
console.log(fileDetails )
.
.
.
});
或者如果您只需要 routes.imageUpload.js
中的上传器,您可以像这样使用中间件,
const express = require("express");
const router = express.Router();
const upload = require("../services/imageUpload");
const singleUpload = upload.single("image");
router.post("/",[singleUpload],function (req, res) {
let fileDetails = req.files
console.log(fileDetails )
.
.
.
});