Skip to content

Commit 144f03b

Browse files
isramosTimer
authored andcommitted
Update doc server example to work from any directory (#1988)
* Node.js serving with absolute path It’s safer to use the absolute path of the directory that you want to serve, in case you run the express app from another directory. * Update README.md
1 parent a0c37ca commit 144f03b

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

packages/react-scripts/template/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -1240,10 +1240,10 @@ const express = require('express');
12401240
const path = require('path');
12411241
const app = express();
12421242

1243-
app.use(express.static('./build'));
1243+
app.use(express.static(path.join(__dirname, 'build')));
12441244

12451245
app.get('/', function (req, res) {
1246-
res.sendFile(path.join(__dirname, './build', 'index.html'));
1246+
res.sendFile(path.join(__dirname, 'build', 'index.html'));
12471247
});
12481248

12491249
app.listen(9000);
@@ -1262,11 +1262,11 @@ If you use routers that use the HTML5 [`pushState` history API](https://develope
12621262
This is because when there is a fresh page load for a `/todos/42`, the server looks for the file `build/todos/42` and does not find it. The server needs to be configured to respond to a request to `/todos/42` by serving `index.html`. For example, we can amend our Express example above to serve `index.html` for any unknown paths:
12631263

12641264
```diff
1265-
app.use(express.static('./build'));
1265+
app.use(express.static(path.join(__dirname, 'build')));
12661266

12671267
-app.get('/', function (req, res) {
12681268
+app.get('/*', function (req, res) {
1269-
res.sendFile(path.join(__dirname, './build', 'index.html'));
1269+
res.sendFile(path.join(__dirname, 'build', 'index.html'));
12701270
});
12711271
```
12721272

0 commit comments

Comments
 (0)