简体   繁体   中英

Get origin domain in NodeJS - Express

I am trying to get the domain from the request in nodejs, the request originates from PHP and when I get origin in Nodejs I get undefined.

This is my code in PHP: (domain1.com)

    $curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://myNodedomain.com/sendAlert',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HEADER => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => array('token' => '3242342332'),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

This is my code in NodeJs: (mynodedomain.com)

app.post('/sendAlert', async (req, res) => {
   console.log(req.get('host'));
   console.log(req.get('origin'));
});

The desired result is: https://domain1.com/

Any solution?

我对 PHP 不是很熟悉,但是添加这样的东西可能会起作用:

curl_setopt($curl, CURLOPT_HTTPHEADER, ['Origin: domain1.com']);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM