Consume API with Javascript and save data in Mongodb

Asked

Viewed 403 times

0

Hello, Currently I have an application that Backend is in PHP and consumes a JSON API. Through the query I can save the data in my Mysql DB. I want to know if it is possible to do this in Javascript.

Follow an example of the code I have;

use GuzzleHttp\Client;
use Illuminate\Support\Facades\Cache;

class BlingClient {
protected $apikey, $guzzle, $url;

public function __construct($apikey)
{
    $this->apikey   = env('BLING_APIKEY');
    $this->guzzle   = new Client();
    $this->url      = env('BLING_URL', 'https://bling.com.br/Api/v2/');
}

public function filters($arr_filters = [])
{
    $filters = [];

    foreach($arr_filters as $k => $arr)
    {
        $filters[] = $k.'[' . $arr . ']';
    }

    $filters = implode(";", $filters);

    return $filters;
}

public function getPedidos($arr_filters = [], $page = 1)
{
    $filters = $this->filters($arr_filters);

    $res = $this->guzzle->request('GET', $this->url . 'pedidos/page=' . $page . '/json/', [
        'query' => [
            'apikey'    => $this->apikey,
            'filters'   => $filters
        ]
    ]);

    $pedidos = json_decode($res->getBody());

    return (!isset($pedidos->retorno->erros))? $pedidos->retorno->pedidos : [];
}

By way of learning, I want to know if it is possible to do this.

  • You want to know if you can do this in the backend with Node?

  • Yeah, there you go. Today my backend is being Laravel and I read some articles saying that Nosql is more scalable, hence my doubt on whether it is possible to do this and if yes, how?

  • Yes, you can, but it’s worth redoing your whole system because of scalability?

  • @Costamilam, I won’t redo it, it’s more for learning title.

1 answer

1

When consulting an API with Insomnia, you can request the code for consultation in several languages, such as: PHP, Curl, Javascript, Nodejs and among others.

So I found the answer to what I was looking for.

Thank you

  • Nodejs is not a line, Node is where the JS language runs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.