Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

89
Visualizações
Outputting HTML in NodeJS is not working with res

I am using a DigitalOcean serverless function with but I am not sure how to output HTML - I can only get it to print to the console.

  const http = require('http');
  const url = require('url');

  const metascraper = require('metascraper')([
    require('metascraper-title')()
  ])

  const got = require('got');

 exports.main = (args) => {
   
  const targetUrl = "https://facebook.com";
   
   (async () => {
     
     res.setHeader("Content-Type", "application/json; charset=utf-8");
     
     try {

       const { body: html, url } = await got(targetUrl).catch(error => console.error(error));
       const metadata = await metascraper({ html, url }).catch(error => console.error(error));
       
       res.end("<h1>Print test</h1>");
       
       // Return the metadata as JSON
       console.log(JSON.stringify(metadata));
     } catch (err) {
       console.log(err);
     }
   })()
   
   
 }

The error is that UnhandledPromiseRejectionWarning: ReferenceError: res is not defined

In the example provided by DigitalOcean, this is how they are able to output text:

exports.main = (args) => {
  return qrcode.toDataURL(args.text).then(res => ({
    headers:  { 'content-type': 'text/html; charset=UTF-8' },
    body: args.img == undefined ? res : `<img src="${res}">`
  }))
}

I have tried using the .then(res... they also used in the example but that does not work either

How can I get text to be outputted to the document? I am not using express or anything, I just modified the example provided by DigitalOcean to get it to work with my own modules - this is a serverless function

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda