当我在 POSTMAN 中发送 POST 时,文件不会上传并且应用程序崩溃。
节点:内部/错误:490 ErrorCaptureStackTrace(错误); ^
TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型或 Buffer 或 URL 的实例。收到空 在 TempWriteStream.WriteStream (node:internal/fs/streams:326:5) 在新的 TempWriteStream (C:\Users\NOOB\Desktop\mern-project ode_modules s-temp\lib\write-stream.js:6:15) 在 Object.createWriteStream (C:\Users\NOOB\Desktop\mern-project ode_modules s-temp\lib emp.js:121:10) 在 Busboy。 (C:\Users\NOOB\Desktop\mern-project ode_modules\multer\lib ead-body.js:70:27) 在 Busboy.emit(节点:事件:513:28) 在 Busboy.emit (C:\Users\NOOB\Desktop\mern-project ode_modules usboy\lib\main.js:37:33) 在 PartStream。 (C:\Users\NOOB\Desktop\mern-project ode_modules usboy\lib ypes\multipart.js:214:13) 在 PartStream.emit(节点:事件:513:28) 在 HeaderParser。 (C:\Users\NOOB\Desktop\mern-project ode_modules\dicer\lib\Dicer.js:50:16) 在 HeaderParser.emit(节点:事件:513:28){ 代码:'ERR_INVALID_ARG_TYPE' }
Node.js v18.15.0 [nodemon] app crashed - waiting for file changes before starting...
PACKAGE.JSON
"name": "mern-project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon server"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"bcrypt": "^5.1.0",
"body-parser": "^1.20.2",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"express-fileupload": "^1.4.0",
"jsonwebtoken": "^9.0.0",
"mongoose": "^6.10.4",
"multer": "^2.0.0-rc.2",
"nodemon": "^2.0.22",
"react-scripts": "5.0.1",
"validator": "^13.9.0"
},
"devDependencies": {
"@svgr/webpack": "^6.5.1"
},
"overrides": {
"@svgr/webpack": "^6.5.1"
}
}
post.routes.js
const router = require("express").Router();
const postController = require("../controllers/post.controller");
const multer = require("multer");
const upload = multer();
router.get("/", postController.readPost);
router.post("/", upload.single("file"), postController.createPost);
router.put("/:id", postController.updatePost);
router.delete("/:id", postController.deletePost);
router.patch("/like-post/:id", postController.likePost);
router.patch("/unlike-post/:id", postController.unlikePost);
// comments
router.patch("/comment-post/:id", postController.commentPost);
router.patch("/edit-comment-post/:id", postController.editCommentPost);
router.patch("/delete-comment-post/:id", postController.deleteCommentPost);
module.exports = router;
user.routes.js
const router = require("express").Router();
const authController = require("../controllers/auth.controller");
const userController = require("../controllers/user.controller");
const uploadController = require("../controllers/upload.controller");
const multer = require("multer");
const upload = multer();
// auth
router.post("/register", authController.signUp);
router.post("/login", authController.signIn);
router.get("/logout", authController.logout);
// user DB
router.get("/", userController.getAllUsers);
router.get("/:id", userController.userInfo);
router.put("/:id", userController.updateUser);
router.delete("/:id", userController.deleteUser);
router.patch("/follow/:id", userController.follow);
router.patch("/unfollow/:id", userController.unfollow);
// upload
router.post("/upload", upload.single("file"), uploadController.uploadProfil);
module.exports = router;
upload.controller.js
const UserModel = require("../models/user.model");
const fs = require("fs");
const { promisify } = require("util");
const pipeline = promisify(require("stream").pipeline);
const { uploadErrors } = require("../utils/errors.utils");
module.exports.uploadProfil = async (req, res) => {
try {
if (
req.file.detectedMimeType != "image/jpg" &&
req.file.detectedMimeType != "image/png" &&
req.file.detectedMimeType != "image/jpeg"
)
throw Error("invalid file");
if (req.file.size > 500000) throw Error("max size");
} catch (err) {
const errors = uploadErrors(err);
return res.status(201).json({ errors });
}
const fileName = req.body.name + ".jpg";
await pipeline(
req.file.stream,
fs.createWriteStream(
`${__dirname}/../client/public/uploads/profil/${fileName}`
)
);
try {
await UserModel.findByIdAndUpdate(
req.body.userId,
{ $set: { picture: "./uploads/profil/" + fileName } },
{ new: true, upsert: true, setDefaultsOnInsert: true },
(err, docs) => {
if (!err) return res.send(docs);
else return res.status(500).send({ message: err });
}
);
} catch (err) {
return res.status(500).send({ message: err });
}
};