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.

App.use does not alter request path for middleware

See original GitHub issue

Describe the bug

The sirv or serve-static middleware does not serve files when putting it under another path in the router. It does work when the mount path is / or corresponds with the filesystem.

To Reproduce

import { App } from "@tinyhttp/app";
import serveStatic from "serve-static";
import sirv from "sirv";

new App()
  .use("/static", sirv("dist/client"))
  .use("/static2", serveStatic("dist/client"))
  .use("/pages", sirv("dist/client"))
  .listen(3000);

$ curl http://localhost:3000/static/pages/index.js
Not found
$ curl http://localhost:3000/static2/pages/index.js
Not found
$ curl http://localhost:3000/pages/index.js
console.log("hi");

Expected behavior

$ curl http://localhost:3000/static/pages/index.js
console.log("hi");
$ curl http://localhost:3000/static2/pages/index.js
console.log("hi");
$ curl http://localhost:3000/pages/index.js
Not found

Versions

  • node: v14.13.0
  • @tinyhttp/app: ^1.0.14

Additional context

$ tree dist/client
dist/client
└── pages
    ├── index.js
    ├── index.js.map
    ├── outdex.js
    └── outdex.js.map

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
talentlessguycommented, Dec 23, 2020

@calumk looks like it’s a subapp issue. Subapp handles all the paths starting with /, leading to paths in the original apps getting ignored

will close this issue and open a new one

1reaction
talentlessguycommented, Dec 19, 2020

ok so what express does is that it strips the path, e.g. /static, from req.url and req.path

but it remains in req.originalUrl

will fix it in a moment 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

When I change url in a middleware, it does not jump to next ...
When I change url in a middleware, it does not jump to next middleware even that middleware match pattern path · You can...
Read more >
Writing middleware for use in Express apps
If myLogger is loaded after the route to the root path, the request never reaches it and the app doesn't print “LOGGED”, because...
Read more >
Conditional Middleware based on request in ASP.NET Core
This post looks at how to configure ASP.NET Core middleware in a way that allows you to have different middleware for different types...
Read more >
Two routers mounted at same path do not isolate their ...
Because middleware are allowed to alter the request path, method, etc., we cannot even know if the request matches a path without executing...
Read more >
Complete Guide to Express Middleware - Reflectoring
When we request a non-existent route, the third error handler is invoked giving us an error message: invalid path . Using Third-Party Middleware....
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