0

I'm struggling to understand how NodeJS works.
It's the first time I'm using NodeJS.
The snippet below is making calls to localhost from the browser, not from the server.
Can someone guide me to how to convert this snippet to make calls from the server instead of the client's browser?

import logo from './logo.svg';
import './App.css';
import React, { Component } from 'react';

class App extends Component {
  constructor(props) {
    super();
    this.state = {
      cpu: 0,
      ram: 0
    }
    this.loadData = this.loadData.bind(this)
  }

componentDidMount() {
    this.loadData()
    setInterval(this.loadData, 300);
 }

async loadData() {
    try {
      const res = await fetch('http://localhost:5000/stats');
      const blocks = await res.json();
      const ram = blocks.ram;
      const cpu = blocks.cpu;
      console.log(ram);
      this.setState({
        cpu, ram
      })
    } catch (e) {
      console.log(e);
   }
}


render() {
    return (
      <div className="App" >
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <div>
            <h3>CPU : {this.state.cpu}</h3>
            <h3>RAM : {this.state.ram}</h3>
          </div>
        </header>
      </div>
    );
  }
}
export default App;

If the question doesn't makes sense, Apologies!

Gaurav
  • 197
  • 2
  • 15
gary rizzo
  • 39
  • 7
  • check out: https://stackoverflow.com/questions/56657934/how-to-read-api-from-one-node-server-to-another-node-server-using-http?rq=1 – Andrea Pollini May 23 '21 at 08:42
  • Let me give it a shot. just fyi the backend api is in python, exposed with gunicorn – gary rizzo May 23 '21 at 08:59
  • I'm not sure if I got you right but React is front-end library working on the browser and Node.js is platform to run JS on the server or somewhere outside of browser (back-end). The whole point is to connect to your back-end (Node.js) from your front-end (browser, your React app) to get data from that and you're doing it right here. These are 2 separated processes that must communicate by some protocol (HTTP here but it doesn't matter for now). But if I get you wrong then take a look at node-fetch library so you can just copy-paste your method to Node.js app and it'll work – Arek C. May 24 '21 at 08:09

1 Answers1

0

Insert the code into the index.js or whatever starting file you use for nodeJS and remove it from the client JS. You might want to send the data using another mode then. You might use EJS (or any other) template engine to create the HTML on the server before sending it.

I see, you have used React. In such a case you might want to use URL parameters to send the data to the client and the client can access those using React, or your client itself can request from your server.

ref: SO post

deekeh
  • 675
  • 1
  • 6
  • 21
  • Do i have the correct approach to using nodeJS for this task? Basically i have 2 containers. One exposing an http api with CPU and MEM usage. The second container is the NodeJS frontend reading from the API (which is hosted locally). I was given this code and have no idea how NodeJS works. By the way, so just take the whole code in the class and paste in index.js ? – gary rizzo May 23 '21 at 08:45
  • 1
    Yes. I think @Andrea has linked a proper post to your question. You want your server to get data from another server and send it to your client here, if I'm not wrong. – deekeh May 23 '21 at 08:51