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!