DEV Community

Discussion on: How to create a React-Node.js application

Collapse
 
maqi1520 profile image
Alex Ma • Edited

I think it's better for deploy

Devlopment

create-react-app package.json support proxy=localhost:3000

Production

// Have Node serve the files for our built React app
app.use(express.static(path.resolve(__dirname, '../client/build')));

// Handle GET requests to /api route
app.get("/api", (req, res) => {
  res.json({ message: "Hello from server!" });
});


// All other GET requests not handled before will return our React app
app.get('*', (req, res) => {
  res.sendFile(path.resolve(__dirname, '../client/build', 'index.html'));
});
Enter fullscreen mode Exit fullscreen mode

jsx

componentDidMount() {
    fetch("http://localhost:3000/api")
      .then(res => res.json())
      .then(data => this.setState({ name: data.name }))
  }
Enter fullscreen mode Exit fullscreen mode
Collapse
 
junko911 profile image
Junko T.

Great! In this case, I will need only Node server to run the application. Thank you for the comment!