Server. Js

const express = require('express');
const path = require('path');
const app = express();
const PORT = process.env.PORT || 5000;

// Middleware to serve static files from the React app
app.use(express.static(path.join(__dirname, 'client/build')));

// API route example
app.get('/api/greet', (req, res) => {
  res.json({ message: 'Hello from the server!' });
});

// Catch-all handler to return the React app for unknown routes
app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname, 'client/build', 'index.html'));
});

// Start the server
app.listen(PORT, () => {
  console.log(`Server is running on http://localhost:${PORT}`);
});

Comments

Popular posts from this blog

Petroleum