Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

90
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!