Skip to content

Commit

Permalink
Refactor server routes for better handling of static files
Browse files Browse the repository at this point in the history
  • Loading branch information
vincenthphung committed Jun 20, 2023
1 parent 141c7bd commit a10cc7f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 127 deletions.
Binary file modified .DS_Store
Binary file not shown.
29 changes: 28 additions & 1 deletion build/index.html
Original file line number Diff line number Diff line change
@@ -1 +1,28 @@
<!doctype html><html lang="en"><head><meta charset="utf-8"/><meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no"/><meta name="theme-color" content="#000000"/><link href="https://fonts.googleapis.com/css?family=Work+Sans:300,500,600" rel="stylesheet"/><link href="https://fonts.googleapis.com/css?family=Poppins:600" rel="stylesheet"/><link rel="manifest" href="/manifest.json"/><link rel="shortcut icon" href="/favicon.ico"/><title>ravenous</title><script defer="defer" src="/static/js/main.b75d2832.js"></script><link href="/static/css/main.62f3928e.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width,initial-scale=1,shrink-to-fit=no"
/>
<meta name="theme-color" content="#000000" />
<link
href="https://fonts.googleapis.com/css?family=Work+Sans:300,500,600"
rel="stylesheet"
/>
<link
href="https://fonts.googleapis.com/css?family=Poppins:600"
rel="stylesheet"
/>
<link rel="manifest" href="/manifest.json" />
<link rel="shortcut icon" href="/favicon.ico" />
<title>ravenous</title>
<script defer="defer" src="/static/js/main.b75d2832.js"></script>
<link href="/static/css/main.62f3928e.css" rel="stylesheet" />
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
</body>
</html>
124 changes: 0 additions & 124 deletions build/reset.css

This file was deleted.

12 changes: 10 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@ app.use(cors());

const apiKey = process.env.API_KEY;

// Serve static files from the React app
app.use(express.static(path.join(__dirname, 'build')));

// Handle the requests for the individual files like main.js, main.css, etc.
app.get(/^\/(static|favicon\.ico|manifest\.json|logo192\.png)/, (req, res) => {
const filePath = path.join(__dirname, 'build', req.url);
res.sendFile(filePath);
});

app.get('/search', (req, res) => {
axios({
method: 'get',
Expand All @@ -26,8 +33,9 @@ app.get('/search', (req, res) => {
});
});

app.get('/*', (req, res) => {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
// The "catchall" handler: for any request that doesn't match one above, send back React's index.html file.
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});

const port = process.env.PORT || 3000;
Expand Down

1 comment on commit a10cc7f

@vercel
Copy link

@vercel vercel bot commented on a10cc7f Jun 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.