Home > Back-end >  How can we call url() with port number in Laravel controller. Node server is running on different po
How can we call url() with port number in Laravel controller. Node server is running on different po

Time:05-21

My Laravel application is running on 127.0.0.1:8000 port but my node server is running on port 127.0.0.1:8001 which has a node script that scrapes data. how can I call the URL function to point out the 127.0.0.1:8001 port from a Laravel controller to run such a script?

for example, check the comment in the index() in DataScrapingController

class DataScrapingController extends Controller
{
    public function index()
    {   
        // Here is the below URL I want to call this URL when a user calls this index 
        //controller function 

        "http://127.0.0.1:8001/scrape"
        


        // After scraping the data when this **http://127.0.0.1:8001/scrape** URL call. which 
        // saves the data in the output.json file here I get the output.json file and return the 
        // data to the data-scraping blade file.

        $data = json_decode(file_get_contents('output.json'));
        return view('data-scraping', compact('data')); 
    }
}

Below image is the node hierarchy in the Laravel application

enter image description here

thanks in advance

CodePudding user response:

Use the Laravel Http wrapper around guzzle

$response = Http::get("http://127.0.0.1:8001/scrape");

Please see https://laravel.com/docs/9.x/http-client#main-content

Make sure you are not running php artisan serve as this can only handle one connection at once.

  • Related