Skip to content

Commit 05f3f5e

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 a0b16df commit 05f3f5e

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
@@ -1242,10 +1242,10 @@ const express = require('express');
12421242
const path = require('path');
12431243
const app = express();
12441244

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

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

12511251
app.listen(9000);
@@ -1264,11 +1264,11 @@ If you use routers that use the HTML5 [`pushState` history API](https://develope
12641264
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:
12651265

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

12691269
-app.get('/', function (req, res) {
12701270
+app.get('/*', function (req, res) {
1271-
res.sendFile(path.join(__dirname, './build', 'index.html'));
1271+
res.sendFile(path.join(__dirname, 'build', 'index.html'));
12721272
});
12731273
```
12741274

0 commit comments

Comments
 (0)