Printing output from a promise via API

Joined
Aug 19, 2020
Messages
1
Reaction score
0
Hi all,

this should be a 'hello world' type of question for all the experienced people out there, but I am struggling to get it working. I'm trying to get CPU temperature readings from a Raspberry Pi using the systeminformation package and then display the output through an express api, in my local network.

This simple code does work and outputs the temperature reading to the console:
JavaScript:
const si = require('systeminformation');

si.cpuTemperature()
    .then(data => console.log(data))
    .catch(error => console.error(error));

But I can't seem to grasp how to output this in a super-simple API. Currently my code is this:

JavaScript:
const si = require('systeminformation');
const express = require('express');
const app = express();
const port = 3389;

app.listen(port, () => {
    console.log(`Server running on port ${port}`);
});

app.get("/", (req, res, next) => {

  res.send(
    si.cpuTemperature()
      .then(data => console.log(data))
      .catch(error => console.error(error)));

});

It does start a server on port 3389, but going to http://ipaddressofmypi:3389/ only gives me "{}" in the browser, then the temperature reading is displayed again in the console... I know I'm close, but I just can't seem to jump through the last hurdle.

Help?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top