0

I try to use the npm qrcode API with express. When the user enters an address into a form, the QR-Code should be displayed on the website. I searched for a long time, but can't figure it out, since I don't understand what to loog for exactly.

I tried the following:

app.post('/ausgabe', async (req, res) => {
  try {
    res.setHeader('200', { 'Content-type': 'application/json' });
    const eingabe = `'${req.body.address}'`;

    const qrImage = await generateQR(eingabe);
    console.log(qrImage);

    const body = `<!DOCTYPE html>
    <html lang="de">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>QR-Code</title>
    </head>
    <body>
        <img ${qrImage} />
    </body>
    </html>`;
    res.send(body);
  } catch (err) {
    res.status(404).json({
      status: 'Fail',
      message: err,
    });
  }
});

I console.logged 'qrImage' and it's there and correct, but there is nothing showing up in the HTML.

Thanks a lot, TobiG

5
  • 1
    You are including whatever the string output of qr image is, can you show what console log qrImage gives you? Commented Oct 19, 2021 at 15:29
  • The snippet editor has four parts. Each has a specific job. Why did you paste your script in the HTML section? Commented Oct 19, 2021 at 15:29
  • I think the content-type is wrong. It should be content-type: text/html;charset=UTF-8 Commented Oct 19, 2021 at 15:36
  • Do you need to put it into a src attribute in the image? <img src="${qrImage}" /> Commented Oct 19, 2021 at 16:04
  • @TJBlackman Thanks, that was the solution. The qrcode API gave me some code that didn't include the 'src=' . Also there was a mistake at const eingabe = '${req.body.address}'; --> the single quotes weren't necessary. Thanks for your help. Commented Oct 20, 2021 at 9:01

0

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.