现在,我正在AWS Lambda上内联编辑代码。这是我目前所拥有的:
var qrImage = require('qr-image');
exports.handler = async (event) => {
return sendRes(200,'hi');
};
/*
const sendRes = (status, body) => {
var response = {
statusCode: status,
headers: {
"Content-Type": "text/html"
},
body: body
};
return response;
};
*/
const sendRes = (status, body) => {
const svg_string = qrImage.imageSync('http://www.nodejs.org',{ type: 'png', size: 20 });
var response = {
statusCode: status,
headers: {
"Content-Type": "image/jpeg"
},
body: svg_string
};
return response;
};
这将输出文本内部服务器错误。
下面的代码创建一个png文件并将其存储。我希望在lambda上使用的功能是在访问端点时仅显示图像(不涉及保存)。但是我不确定该怎么做(Lamda的新功能,Node.js)。我到底应该看什么?
var qrImage = require('qr-image');
var fs = require('fs');
qrImage
.image("http://www.nodejs.org", {type:'png', size:20})
.pipe(fs.createWriteStream("MyQRCode.png"));
编辑 我做了一些小改动,这似乎行得通
var qrImage = require('qr-image');
exports.handler = async (event) => {
return sendRes(200,'hi');
};
const sendRes = (status, body) => {
const svg_string = qrImage.imageSync('this is AWS!', { type: 'svg', size: 10 });
var response = {
statusCode: status,
headers: {
"Content-Type": "image/svg+xml"
},
body: svg_string
};
return response;
};
答案 0 :(得分:1)
如果仅在lambda的帮助下返回图像,则同步也将起作用,这里是代码
const sendRes = (status, body) => {
const svg_string = qr.imageSync('http://www.nodejs.org', { type: 'png', size: 20 });
var response = {
statusCode: status,
headers: {
"Content-Type": "image/jpeg"
},
body: svg_string
};
return response;
};