Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Send data from Companion to localhost with NodeJS

Hello, a year ago I transferred data from companion to a server with this code: 

 

// Listen for the onmessage event
messaging.peerSocket.onmessage = function(evt) {
  // Output the message to the console
  console.log(JSON.stringify(evt.data));
  // Send information to the server
  fetch('https://XXXX.es:3000/heartRate', {
    method:'post',
    headers: {
     'Content-Type': 'application/json'
    },
    body:JSON.stringify(evt.data)
  }).then(function(result) {
    if (!result.ok) {
      console.log(result.status + ': ' + result.statusText);
      return;
    }
    console.log('Enviado paquete');
  }).catch(function (err){
    console.log(err)
  })
}

Now i can't use that server anymore and i would like to fetch to localhost:3000. It's where my nodeJS server can listen

//nodeJS server.js code
[...]
https.createServer(options, app).listen(port, (err) => {
   if (err) {
     return console.log('something bad happened', err)
   }

   console.log(`server is listening on ${port}`) });
[...]

Is there a possibility to send data to my localhost through the companion? Later I need to insert that data in a mongo database with that server.js

 

Best Answer
1 REPLY 1

The companion runs on the phone, so "localhost" is the phone. You'd need your phone and computer to be on the same network, then use the ip address of your computer instead of localhost.

Best Answer