1

Right now I am editing the code inline on AWS lambda. This is what I have currently:

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;
};

This outputs the text Internal server error.

The code below creates a png file and stores it. The functionality I would like on lambda is to simply display the image(No saving involved) when I access the endpoint. But I am not sure how to go about it (New to Lamda, Node.js). What exactly should I be looking into?

var qrImage = require('qr-image');
var fs = require('fs');

qrImage
    .image("http://www.nodejs.org", {type:'png', size:20})
    .pipe(fs.createWriteStream("MyQRCode.png"));

EDIT I made some small changes and this appears to be working

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;
};

1 Answer 1

1

If you are only returning the image with the help of lambda then sync will also work, here the code for it

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;
};
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for the quick reply. Just an additional question, do I have to upload the code as a zip with the libraries or can I manually add the library to the inline editor? The second seems easier but I am not sure if it will work.
I guess you are asking about lambda? If so let me clear first I don't have much experience on lambda, I have seen people directly pushing code with all dependencies to lambda in a zip.
Yes this lamda thing is new to me as well. I am trying that right now.
Hey I'll update my question. For some reason I am getting internal server error. I do not have cloud watch logs set up so it does not show the exact reason why it is not working but I'll try to get the logs.
I made some changes and it is working now. Thanks once again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.