Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I use the middleware on a path other than '/' ? #3098

Open
wolfgang42 opened this issue May 28, 2019 · 6 comments
Open

How do I use the middleware on a path other than '/' ? #3098

wolfgang42 opened this issue May 28, 2019 · 6 comments

Comments

@wolfgang42
Copy link

❔ Question

How can I tell the Bundler middleware to serve out of a subdirectory path?

I'd like to serve a Parcel-bundled app as a subdirectory of an Express app. For production I plan on just pointing Nginx to the dist/ folder, but for development I'd like to integrate the Parcel middleware into the main server, with hot-reloading etc. I've therefore followed the API docs and attached a new Bundler().middleware() to the existing Express app, and told the Bundler what its public URL is:

app.use('/app/', new Bundler(__dirname+'/../browserclient/src/index.html', {
	publicUrl: '/app/',
}).middleware())

Unfortunately, this just serves the index page for everything under /app/, including the JS and CSS bundles. This appears to be because of

} else if (
!pathname.startsWith(bundler.options.publicURL) ||
path.extname(pathname) === ''
) {
// If the URL doesn't start with the public path, or the URL doesn't
// have a file extension, send the main HTML bundle.
return sendIndex();
which checks if the middleware is serving a known path under options.publicURL. Unfortunately since I'm using app.use(path, ...) the pathname variable is relative to /app/ (that is, it goes /index.deadbeef.js, not /app/index.deadbeef.js) and so never matches, and the bundled code can never be served. If I just tell it to app.use(new Bundler()) then it works, but Parcel also takes over the rest of the server and starts handing out its index instead of 404 pages.

🌍 Your Environment

Software Version(s)
Parcel 1.12.3
Node 8.9.4
Express 4.13.4
@Kogia-sima
Copy link
Contributor

I think you can change req.url by assigning req.originalUrl.

const parcel_middleware = new Bundler(__dirname+'/../browserclient/src/index.html', {
	publicUrl: '/app/',
}).middleware();

app.use('/api', function(req, res, next) {
    req.url = req.originalUrl;
    parcel_middleware(req, res, next);
});

@Kogia-sima
Copy link
Contributor

@Kogia-sima
Copy link
Contributor

But this is confusing. It is better if Parcel use req.originalUrl if it is defined, instead of using req.url, with which Parcel cannot detect the middleware is used for root (/) path or another url.

@zocky
Copy link

zocky commented Jun 2, 2019

The problem with the example solution at github is that it doesn't work at /path, only at /path/.

And anyway, isn't this what .publicUrl is for, and this is already supposed to be working out of the box? I.e. isni't this a bug, or am I misunderstanding what .publicUrl is for?

@wolfgang42
Copy link
Author

@mischnic Any chance this can get backported also? As a workaround I have a bunch of extra terminal windows running parcel instances, but it'd be a lot easier if this could all be part of my main server program.

@stale
Copy link

stale bot commented Jan 17, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 30 days if no further activity occurs. Thank you for your contributions.

@stale stale bot added the Stale Inactive issues label Jan 17, 2020
@DeMoorJasper DeMoorJasper removed the Stale Inactive issues label Jan 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants