question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

❔ 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 https://github.com/parcel-bundler/parcel/blob/0b98eedf2d9cb1a5232329d0d41e36bdef2b87bd/packages/core/parcel-bundler/src/Server.js#L57-L63 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

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
Kogia-simacommented, May 29, 2019

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.

1reaction
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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use specific middleware in Express for all paths except a ...
I would add checkUser middleware to all my paths, except homepage. app.get('/', routes.index); app.get('/account', checkUser, routes.account);.
Read more >
Writing middleware for use in Express apps
To load the middleware function, call app.use() , specifying the middleware function. For example, the following code loads the myLogger middleware function ...
Read more >
Complete Guide to Express Middleware - Reflectoring
We can use middleware functions for different types of processing tasks ... Route for handling get request for path / app.get('/', (request, ...
Read more >
Conditional Middleware based on request in ASP.NET Core
Optionally, you can add other arguments to your Invoke method and these dependencies will be resolved on each request using the baked in...
Read more >
Middleware - Laravel - The PHP Framework For Web Artisans
Additional middleware can be written to perform a variety of tasks besides authentication. For example, a logging middleware might log all incoming requests...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found