React React is a JavaScript library
React React is a JavaScript library
It allows you to
create reusable UI components and efficiently update the UI when the underlying
data changes.
npm init -y
Create a new file called server.js and add the following code:
app.use(express.json());
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
Open the src/App.js file and replace the existing code with the following:
import React, { useEffect, useState } from 'react';
function App() {
const [users, setUsers] = useState([]);
useEffect(() => {
fetch('/api/users')
.then((response) => response.json())
.then((data) => setUsers(data));
}, []);
return (
<div>
<h1>User List</h1>
<ul>
{users.map((user) => (
<li key={user.id}>
{user.name} - {user.email}
</li>
))}
</ul>
</div>
);
}
Open the server.js file and add the following code to define a route for fetching
users: