打字稿表达图像上传有云问题

时间:2019-01-12 17:31:13

标签: node.js typescript express cloudinary

无法将图片上传到cloudinary。我正在使用带有路由控制器模块的Typescript node.js express。我正在从常规javascript迁移到打字稿,在常规js文件上传中效果很好,但在打字稿中却无法正常工作。

import multer from "multer";
import cloudinary from "cloudinary";
import cloudinaryStorage from "multer-storage-cloudinary";
import config from "config";

cloudinary.config({
  cloud_name: config.get("CLOUD_NAME"),
  api_key: config.get("CLOUDINARY_API_KEY"),
  api_secret: config.get("CLOUDINARY_SECRET")
});

const storage = cloudinaryStorage({
  cloudinary: cloudinary,
  folder: "demo",
  allowedFormats: ["jpg", "png"],
  transformation: [{ width: 500, height: 500, crop: "limit" }]
});

export const parser = multer({ storage: storage });

图片上传控制器


  import { parser } from "../helpers/gymUploadHelper";

  @Post("/files")
  public async uploadGymImage(@Req() req: Request, @Res() res: Response) {
    const errors: any = {};
    const upload = parser.single("image");

    upload(req, res, err => {
      if (err) {
        errors.image = "Error when uploading file";
        return res.status(501).json(errors);
      } else {
        return res.status(200).json(req.file.secure_url);
      }
    });
  }

在调用带有图像的端点时出现此错误:

Error
    at NotFoundError.HttpError [as constructor] (/home/xxx/Files/Projects/Gymr/server/node_modules/routing-controllers/http-error/HttpError.js:27:23)
    at new NotFoundError (/home/xxx/Files/Projects/Gymr/server/node_modules/routing-controllers/http-error/NotFoundError.js:20:28)
    at ExpressDriver.handleSuccess (/home/xxxx/Files/Projects/Gymr/server/node_modules/routing-controllers/driver/express/ExpressDriver.js:289:23)
    at /home/xxx/Files/Projects/Gymr/server/node_modules/routing-controllers/RoutingControllers.js:128:61

但是它应该返回图像响应网址(例如https://cloudinary.com/img.jpg

0 个答案:

没有答案