React 应用程序 - 文件上传在本地主机上有效,但在 nginx 服务器上无效

React application - file upload works on localhost but not on nginx server

希望能帮到你。

当我 运行 我在本地主机上的应用程序时,以下代码有效,文件正确上传到应用程序根目录中的上传文件夹。但是,当我尝试从我的实时网站上传文件时出现错误。

onst upload = multer({  // Set up multer module to move uploaded file an rename the file
  storage: multer.diskStorage({
    destination(req, file, cb) {
      cb(null, 'uploads/'); //Sets the destination for the new file
    },
    filename(req, file, cb) {
      cb(null, `${new Date().getTime()}_${file.originalname}`); // renames file with the date, time and its original name
    }
  }),
  limits: {
    fileSize: 1024 * 1024 * 5  // Sets the max file size
  },
  fileFilter: (req, file, cb) => {// Only allows image files uploaded
    if (file.mimetype == "image/png" || file.mimetype == "image/jpg" || file.mimetype == "image/jpeg" || file.mimetype === 'image/gif' || file.mimetype ==='image/jfif' ) {
      cb(null, true);
    } else {
      cb(null, false);
      return cb( new Error('Only .png, .jpg and .jpeg format allowed!' ));
      
    }
  }
});

这是我在运行时遇到的错误错误:ENOENT:没有那个文件或目录,打开'build/public/uploads/1619823055971_GettyImages-640318118-c83a508.jpg'

我 运行 为 React 构建后,是否需要为上传设置一个文件夹。刚才我在项目的根目录下只有一个上传文件夹。

感谢您的建议

看来我没有重新启动 pm2,所以它没有显示我对 express 文件所做的任何更改。