Skip to content

Commit

Permalink
Update doc server example to work from any directory (#1988)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
isramos authored and Timer committed Apr 19, 2017
1 parent a0c37ca commit 144f03b
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions packages/react-scripts/template/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1240,10 +1240,10 @@ const express = require('express');
const path = require('path');
const app = express();

app.use(express.static('./build'));
app.use(express.static(path.join(__dirname, 'build')));

app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, './build', 'index.html'));
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});

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

```diff
app.use(express.static('./build'));
app.use(express.static(path.join(__dirname, 'build')));

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

Expand Down

0 comments on commit 144f03b

Please sign in to comment.