I have the following code block
server.get('/energy', (req, res, next) => {
{
const getScript = (url) => {
return new Promise((resolve, reject) => {
const http = require('http'),
https = require('https')
let client = http
if (url.toString().indexOf('https') === 0) {
client = https
}
client.get(url, (resp) => {
let data = ''
// A chunk of data has been recieved.
resp.on('data', (chunk) => {
data += chunk
})
// The whole response has been received. Print out the result.
resp.on('end', () => {
resolve(data)
})
}).on('error', (err) => {
reject(err)
})
})
};
(async (url) => {
console.log('Async ' + await getScript(url))
})('http://localhost:81/energia/monitoramento/central.php?ip=147.1.3.3')
const ret = async (url) => {
const dados = await getScript(url)
console.log(url)
console.log('retorno: ' + dados)
return dados
}// ('http://minhaurl.com')
console.log('Dados de retorno: ' + ret('http://minhaurl.com'))
res.send(ret('http://minhaurl.com'))
next()
}
})
Where I would like to return the value that a certain page brings.
For example, the
(async (url) => {
console.log(await getScript(url))
})('http://minhaurl.com')
At the console it looks like this:
Async [129.66,30.27,3924.31,2721.89,0.69,0.00,0.00,0.00,2826.94,0.00,129.56,31.45,4075.07, -2453.22, -0.60,0.00,0.00,0.00,3253.91,0.00,128.81,29.19,3760.38, - 514.79, -0.14, -1.00, -1.00, -1.00.3724.98,59.76,30.60,35.10]
Nestre excerpt
const ret = async (url) => {
const dados = await getScript(url)
console.log(url)
console.log('retorno: ' + dados)
return dados
}
also looks like this: etorno:
[129.59,30.44,3944.05,2768.30,0.70,0.00,0.00,0.00,2809.28,0.00,129.60,31.92,4137.48, -2462.13, -0.60,0.00,0.00,0.00,3325.15,0.00,128.81,29.33, 3778.27, -558.25, -0.15, -1.00, -1.00, -1.00.3736.80,59.86,30.60,35.00]
I'm trying to show this data on the page with this
console.log('Dados de retorno: ' + ret('http://localhost:81/energia/monitoramento/central.php?ip=147.1.3.3'))
res.send(ret('http://localhost:81/energia/monitoramento/central.php?ip=147.1.3.3'))
next()
But the return says like this:
Return data: [object Promise]
And the page appears only like this: {}
How do I show the same data that appears in the console on the page?
Async [129.66,30.27,3924.31,2721.89,0.69,0.00,0.00,0.00,2826.94,0.00,129.56,31.45,4075.07, -2453.22, -0.60,0.00,0.00,0.00,3253.91,0.00,128.81,29.19,3760.38, - 514.79, -0.14, -1.00, -1.00, -1.00.3724.98,59.76,30.60,35.10]