-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThirdPractice.js
More file actions
102 lines (80 loc) · 2.95 KB
/
ThirdPractice.js
File metadata and controls
102 lines (80 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
https://ibb.co/j6CWrHp
const http = require('http')
const fs = require('fs')
http.createServer(function(req,res){
fs.readFile("DemoFile.html",function(err,data){
res.writeHead(200,{'Content-type':'text/html'})
res.write(data)
return res.end()
})
}).listen(3000)
/*
To get a response from a Node.js server to a React.js frontend, you'll typically use HTTP requests. Here's a basic example of how to make an HTTP GET request from a React.js component to a Node.js server using the popular axios library for simplicity. First, you need to install axios if you haven't already:
bash
Copy code
npm install axios
Next, you can create a React component to make the request. Here's an example:
javascript
Copy code
import React, { Component } from 'react';
import axios from 'axios';
class App extends Component {
constructor() {
super();
this.state = {
data: null,
};
}
componentDidMount() {
// Make an HTTP GET request to your Node.js server
axios.get('http://your-node-server-url/api/data')
.then((response) => {
// Handle the successful response here
this.setState({ data: response.data });
})
.catch((error) => {
// Handle any errors that occurred during the request
console.error(error);
});
}
render() {
return (
<div>
<h1>Response from Node.js Server</h1>
{this.state.data ? (
<pre>{JSON.stringify(this.state.data, null, 2)}</pre>
) : (
<p>Loading...</p>
)}
</div>
);
}
}
*/
/*
export default App;
In this example:
We import the axios library.
We create a componentDidMount lifecycle method where we make an HTTP GET request to your Node.js server. Replace 'http://your-node-server-url/api/data' with the actual URL of your Node.js server and the route you want to fetch data from.
When the request is successful, we update the React component's state with the received data.
In the render method, we display the data or a "Loading..." message based on the state.
On the Node.js side, you need to set up a route that handles the GET request and sends back a response with the data you want to retrieve. Here's a very basic example using Express.js:
javascript
Copy code
const express = require('express');
const app = express();
app.get('/api/data', (req, res) => {
const responseData = {
message: 'Hello from Node.js!',
data: [1, 2, 3, 4, 5],
};
res.json(responseData);
});
app.listen(3001, () => {
console.log('Node.js server is running on port 3001');
});
In this Node.js example:
We create an Express.js server.
We define a route at /api/data that responds with a JSON object when accessed.
We start the server listening on port 3001.
Now, when you run your React.js app and access it in your web browser, it will make a GET request to your Node.js server and display the response data. Make sure to adjust the URLs and data as needed for your specific use case. */