Printing output from a promise via API

Discussion in 'Javascript' started by jursky, Aug 19, 2020.

  1. jursky

    jursky

    Joined:
    Aug 19, 2020
    Messages:
    1
    Likes Received:
    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:
    Code (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:

    Code (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?
     
    jursky, Aug 19, 2020
    #1
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.