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