Comments (10)
Thanks for reporting this issue. The bug is caused due to the way the req.url
property is set when a *
is present
tinyhttp/packages/app/src/app.ts
Line 368 in ed51d60
A PR is welcome
from tinyhttp.
I'll look at making a PR but I'm not sure about 2 things:
- When the path is
/foo/:pat
and the url is/foo/bar
is it expected thatreq.url
becomes/
(I would have expected the URL to remain unchanged)? - When the path is
/foo/*
and the url is/foo/bar
shouldreq.url
become/foo
or/foo/bar
or/bar
?
from tinyhttp.
When the path is /foo/:pat and the url is /foo/bar is it expected that req.url becomes / (I would have expected the URL to remain unchanged)?
As per express, req.url
is /foo/bar
when the path is /foo/:param
When the path is /foo/* and the url is /foo/bar should req.url become /foo or /foo/bar or /bar?
Since the aim is to offer similar APIs to express, req.url
should output /foo/bar
.
from tinyhttp.
Here's the express code
import express from 'express'
const app = express()
app.get('/foo/:path',(req,res) => {
console.log(req.url, req.params);
res.send(`@${req.url}`);
});
app.get('/hello/*',(req,res) => {
console.log(req.url, req.params);
res.send(`@${req.url}`);
});
app.listen(3000);
from tinyhttp.
Thanks for reporting - seems like a very simple bugfix - PR is welcome as said above
from tinyhttp.
@aarontravass What you descibe as express's spec is not what I observe with tinyhttp:
// shows @/ on /x/bar
app.get('/x/:y',(req,res) => {
res.send(`@${req.url}`);
});
This is probably a different issue from the one I mentioned above, which I'm reproducing here:
// shows @/ar on /foo/bar
app.get('/foo/*',(req,res) => {
res.send(`@${req.url}`);
});
Finally I'm seeing a similar but maybe unrelated behavior:
let urls = [];
const router = (req,res,next) => {
urls.push(req.url);
next();
}
// shows @/bar @/ar @/ on /z/bar
app.use('/z',router,router,router,(req,res) => {
res.send(`@${urls.join(' @')}`);
urls = [];
});
I'm guessing all of the above needs to be fixed but I'm waiting to get your input on that.
from tinyhttp.
Looks like the code for req.url
when a param is present is broken as well. The buggy line is
tinyhttp/packages/app/src/app.ts
Line 365 in ed51d60
Finally I'm seeing a similar but maybe unrelated behavior:
This issue is caused by modifying the original request object. Essentially, Line 368, as mentioned by your original post, keeps stripping the first character and it does so for all middle wares. Good catch!
from tinyhttp.
I could use some help navigating the code. I don't understand how handle
, as it is today, differentiates between a route (app.get
, etc) and a middleware (app.use
). Intuitively:
- Within a route, the URL should be left as-is, with no modifications (except removing the query string).
- Within a middleware, the URL should be stripped of its
path
.
Replacing the current logic with the logic I describe above is quite a big change so I'd like to check with you that it makes sense:
- The current logic including checking for
includes(':')
and stripping the url of the path should be removed. - It should be replaced by the logic above.
Is this right?
from tinyhttp.
I don't understand how handle, as it is today, differentiates between a route (app.get, etc) and a middleware (app.use).
There is not much difference between a route and a middleware. In fact, if you look at packages/router/src/index.ts, you'll find that routes are pretty much similar to middlwares.
tinyhttp/packages/router/src/index.ts
Line 202 in 585633f
The only difference is that routes are 'mounted' on the app itself and hence, do not need an extra path variable (also called fullPath
in the code) while middlwares do. This is due to the fact that you can mount middlewares such as subapps onto a specific path.
Replacing the current logic with the logic I describe above is quite a big change so I'd like to check with you that it makes sense:
Yes, your idea is correct.
from tinyhttp.
Replacing the current logic with the logic I describe above is quite a big change so I'd like to check with you that it makes sense:
I believe the fix should be pretty simple since all you need to do is differentiate correctly between a route and a middlware.
from tinyhttp.
Related Issues (20)
- Response object interface doesn't match @types/express behavior, it's not really generic HOT 6
- Simple Pull Request - License-none
- Failed to start https server HOT 1
- Multiple path arguments (as array) to route HOT 4
- Link CONTRIBUTING.md in README.md and mention forking as the first step instead of clone HOT 1
- Incompatible with Express middleware: `express.Handler` HOT 26
- Mounting a subapp on array of paths
- Add missing `accepts` TypeScript signatures HOT 4
- Mallformed URI's leads to crash with `/thing/:id` routes HOT 1
- app.use(<path>, <Router>) is Incompatible with Express HOT 8
- Exporting a router file is not working HOT 7
- Create a community server HOT 8
- The website is down HOT 7
- Request `.xhr` misses fetch requests (headers are lowercase) HOT 3
- req.route always returns undefined HOT 19
- Server instance (of `@tinyhttp/app`) hangs on test since 2.0.33 HOT 4
- Predefined error type HOT 4
- ZOD support / example HOT 6
- [docs] outdated app.engine example with eta HOT 2
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 tinyhttp.