Return value of a request!

0

I'm practicing a bit of nodeJS, so I decided to create an application for the temperature query, however I came across a question. How do I return the value of the weather variable:

const tempo = () =>{
    request('http://apiadvisor.climatempo.com.br/api/v1/locale/city?name=Joinville&state=SC&token=${TOKEN}', (error, response, body) =>{ 
        const id = JSON.parse(body)[0]["id"]
        request('http://apiadvisor.climatempo.com.br/api/v1/forecast/locale/${id}/days/15?token=${TOKEN}', (error,response, body) =>{
            const weather = JSON.parse(body)
        })
  })
}
    
asked by anonymous 23.08.2018 / 02:37

1 answer

0

If by "returning" you mean "serve as a response to a endpoint ", you can use express as follows:

const express = require('express');
const fetch = require('node-fetch');
const app = express();

const TOKEN = '[SEU TOKEN]';

app.get('/', async (req, res) => {
  try {
    const result = await fetch('http://apiadvisor.climatempo.com.br/api/v1/locale/city?name=Joinville&state=SC&token=${TOKEN}');
    const { 0: { id } } = result.text();
    const body = await fetch('http://apiadvisor.climatempo.com.br/api/v1/forecast/locale/${id}/days/15?token=${TOKEN}');
    res.send(JSON.parse(body.text()));
  } catch(e) {
    console.error(e);
    res.status(500).send('Ocorreu um erro interno.');
  }
});

app.listen(3000, () => console.log('A aplicação está sendo executada na porta 3000!'));

Installing express in your application can be done using the command below:

npm install express

And the installation of the node-fetch module can be done through the command:

npm install node-fetch

The service can be accessed through:

http://localhost:3000/

Note: Since you provided an image instead of the code, typing errors may occur. It is important that you do not put in your question the image of the code but rather the code itself, making it easier for anyone to respond.

    
23.08.2018 / 07:31