Firebase端点的云功能处理图像

时间:2017-05-25 23:49:58

标签: javascript firebase image-processing multer google-cloud-functions

我想创建一个云端功能端点,可以接受和处理JPEG等图像文件。这是我的代码:

var express = require('express')
var multer  = require('multer')
//firebase complains that '/user_code/tmp' is read-only if I use this
//var upload = multer({ dest: 'tmp/' })
var upload = multer({
  storage: multer.MemoryStorage,
  fileSize: 5 * 1024 * 1024
});
var app = express()

app.post('/image-upload', upload.single('image'), function(req, res, next) {

   console.log("request data: " + JSON.stringify(req.data));
   console.log("request params: " + JSON.stringify(req.params));
   console.log("request query: " + JSON.stringify(req.query));
   console.log("request body: " + JSON.stringify(req.body));
   console.log("request headers: " + JSON.stringify(req.headers));
   console.log("req.file: " + req.file);
   console.log("req.files: " + req.files);

   res.status(201).send(`My Server Response is ${req.file}`);
});

module.exports.testEndpoint = functions.https.onRequest(app);

出于某种原因,req.file和req.files总是undefined。但是,req.body似乎确实包含我的图像。我正在使用Postman发送HTTP POST请求。

Postman Input and Response

1 个答案:

答案 0 :(得分:0)

我明白了。我的#34;标题"不对。请务必取消选中Content-Type字段。

Uncheck Content-Type in Postman