我想让AWS API Gateway以图片的形式回复。我的Serverless Lambda代码如下:
const express = require('express');
const serverless = require('serverless-http');
const bodyParser = require('body-parser');
const request = require('request');
const fetch = require('node-fetch')
var Jimp = require('jimp');
const app = express()
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.get('/image/:id', async(req, res) => {
const id = req.params.id;
var imgUrl = 'https://developer.salesforce.com/forums/profilephoto/729F00000005O41/T';
let options = {};
const image = await Jimp.read(imgUrl);
image.getBuffer(Jimp.MIME_JPEG, (err, buffer) => {
res.set('content-type', 'image/jpeg');
res.send(buffer.toString('base64'));
});
});
// wrap express app instance with serverless http function
module.exports.handler = serverless(app)
serverless.yml
provider:
name: aws
runtime: nodejs8.10
stage: dev
region: us-east-1
memorySize: 512
custom:
apigwBinary:
types: #list of mime-types
- 'image/jpg'
- 'image/jpeg'
- 'image/png'
functions:
avatarFunc:
handler: index.handler
events:
- http:
path: image/{id}
method: get
contentHandling: CONVERT_TO_BINARY
plugins:
- serverless-offline
- serverless-apigw-binary
这里返回的图像是一个黑盒子。
![这里输入图片描述](https://istack.dev59.com/YI6GU.webp)
Buffer.from`` res.send``res.end
的修订。 AGW /服务器级别! 哎呀。 谢谢! - sonjz