Commit 05f3f5ee authored by Igor Ramos's avatar Igor Ramos Committed by Joe Haddad
Browse files

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
parent a0b16df5
3 merge requests!12191Lim.Pisey.168:/Identified - We are currently investigating reports of missing build logs. The issue has been identified and a resolution is in progress. We will provide a further update when available.Mar 21, 09:02 UTC,!12853brikk,!5717Automatically extract project file structure from build bundle file
Showing with 4 additions and 4 deletions
+4 -4
...@@ -1242,10 +1242,10 @@ const express = require('express'); ...@@ -1242,10 +1242,10 @@ const express = require('express');
const path = require('path'); const path = require('path');
const app = express(); const app = express();
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'));
}); });
app.listen(9000); app.listen(9000);
...@@ -1264,11 +1264,11 @@ If you use routers that use the HTML5 [`pushState` history API](https://develope ...@@ -1264,11 +1264,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: 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 ```diff
app.use(express.static('./build')); app.use(express.static(path.join(__dirname, 'build')));
-app.get('/', function (req, res) { -app.get('/', function (req, res) {
+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'));
}); });
``` ```
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment