Comments (11)
@AndyOGo's solution looks like it wasn't tested, because with it I still get the same issue.
Here is my abridged serve.json
:
{
...
"rewrites": [
{
"source": "/some/path/:path+",
"destination": "/:path"
}
]
}
And here is my abridged curl
output after running npx serve
:
user@Machine project-root % curl -I http://localhost:3000/favicon.png
HTTP/1.1 200 OK
...
user@Machine project-root % curl -I http://localhost:3000/some/path/favicon.png
HTTP/1.1 200 OK
...
user@Machine project-root % curl -I http://localhost:3000/assets/vendor.822f96e6.js
HTTP/1.1 200 OK
...
user@Machine project-root % curl -I http://localhost:3000/some/path/assets/vendor.822f96e6.js
HTTP/1.1 404 Not Found
...
from serve-handler.
This is definitely a very common task that one need to perform
Imagine you have a large organization running a commercial website call it acme.com
There are multiple React Apps, written by multiple teams, deployed to this website, for example:
- app1
- app2
Say the network admin has configured requests to go from acme.com/app1 to go to host1 and acme.com/app2 to host2
Where host1/host2 are machines running serve-handler
...
from serve-handler.
shameless self-promotion alert:
this feature is fixed in my @warren-bank/serve
fork of serve
notes:
- rewrite rules now require an
engine
attribute..
which permits you to choose which methodology/library is used to:- match the url pathname against a pattern
- replace capture groups
from serve-handler.
@erfangc I know that this is pretty common task. Thats why I want to do that, however, It seemed that this is not possible using serve.
Can you provide an example of how to write that correctly?
from serve-handler.
@PetrykowskiM I am in your boat, I am saying this is a fairly common practice so I imagine there must be a way to do it
I've tried a variety of things without much success either
from serve-handler.
Ok, good to know than! Well, would be nice if anybody else could help.
from serve-handler.
@PetrykowskiM I am find many things difficult with this one
first -
rewrites do not really work ... so that sucks
second -
I tried structuring the directory such that host/app/index.html
corresponds to the local FS app/index.html
The second approach succeeded (but you need to disable directoryList
) through the serve.json
config
But then (I am assuming you are writing a SPA in Angular/React/Vue) ... then if you are using any Router(s) in the SPA, you are going to run into all sorts of unexpected problems. This is seemingly benign task is proving to be surprisingly difficult. I hope I am just being stupid here and missing something obvious
from serve-handler.
Seems like the proposal in #27
from serve-handler.
path-to-regexp
supports repeated segments
const toPathRepeated = compile("/:segment+"); toPathRepeated({ segment: "foo" }); //=> "/foo" toPathRepeated({ segment: ["a", "b", "c"] }); //=> "/a/b/c"
I think that should be the solution 🤔
{
"rewrites": [{
"source": "/some/frontend/:path+",
"destination": "/:path"
}]
}
from serve-handler.
Any updates here?
from serve-handler.
If you want to remove prefixes from in-app requests, continue using rewrites
. That works fine.
The
rewrites
is intended to rewrite the url routing within the app andserve
cannot know when the initial static path is requested.
If you want to serve static js/css files from a path different than /static, here is the solution.
Assume you want to serve your frontend under foo.com/frontend
.
- In your build directory, move the
static
folder tofrontend/static
. - Change
index.html
to point new static paths, e.g.:
/static/js/main.xyz.js
to/frontend/static/js/main.xyz.js
and
/static/css/main.xyz.css
to/frontend/static/js/main.xyz.css
- Run
serve
. Make sure the request initiator chain points to/frontend/static/...
:
from serve-handler.
Related Issues (20)
- 416 error with valid range header HOT 1
- Content-Length header missing on GET but present on HEAD request HOT 1
- Request losts when POSTing redirected url HOT 2
- This is not an issue but with High Vulnerable on one of dependency
- If vercel handler is served under a path like /blog/ inside nginx then trailingSlash option redirects will redirect to top level domain name HOT 1
- -S parameter doesn't work on Linux machine.
- How to use custom domain and HTTPS?
- Symlinks option not working as expected in serve.json
- ✨ Configure base path via CLI option
- serve not using 404.html file to display 404 errors
- Can I convert the displayed filename?
- .html is stripped along with query params from URL HOT 2
- Vulnerability in minimatch 3.0.4 HOT 2
- # [Codecov](https://codecov.io/gh/zeit/serve-handler/pull/75?src=pr&el=h1) Report
- Ambiguous readme HOT 1
- Can't add headers to sourcemap files HOT 1
- Rewrite not working in v14.2.0 HOT 2
- Module `punycode` is deprecated since Node.js 21 HOT 4
- Problems with UTF characters
- Update minimatch to resolve security vulnerability
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from serve-handler.