-
Hi. Codeimport uWS from 'uWebSockets.js';
const app = uWS.App();
app.get('/', (res) => {
res.end('hello world');
});
app.get('/foo/', (res) => {
res.end('hello at /foo/');
});
app.get('/bar', (res) => {
res.end('hello at /bar');
});
app.listen(
4000,
(token) => token && console.debug('server started at port', 4000)
); Test
QuestionIs there possible to define path which works both with Thanks for best project |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments
-
trailing slash is considered a different URL so it's not a bug, you could check for trailing slash in your handler and remove it or redirect to non-trailing slash url: // remove trailing slash before processing request
app.any('/*', (res, req) => {
const method = req.getMethod()
let path = req.getUrl()
if(path.endsWith('/') && path != '/') path = path.slice(0,-1)
switch(method) {
case 'get': switch(path) {
case '/': return res.end(`get ${path}`)
case '/foo': return res.end(`get ${path}`)
case '/bar': return res.end(`get ${path}`)
} break
case 'post': switch(path) {
case '/': return res.end(`post ${path}`)
case '/foo': return res.end(`post ${path}`)
} break
}
res.writeStatus('404').end()
}) // redirect to non-trailing slash url
app.any('/*', (res, req) => {
const host = req.getHeader('host')
const path = req.getUrl()
const query = req.getQuery()
if(path.endsWith('/') && path != '/') return res.writeStatus('301').writeHeader('Location',`https://${host}${path.slice(0,-1)}${query?'?'+query:''}`).end()
}) |
Beta Was this translation helpful? Give feedback.
-
@hst-m Thanks for temporarily solution, but it worked before (not remember on which version). |
Beta Was this translation helpful? Give feedback.
-
I fixed it in my project new release with hot-fix temporarily solution. I'm think will be correct if this behavior fixed, but i may be wrong as i don't have enough experience in HTTP Specs. |
Beta Was this translation helpful? Give feedback.
-
ok just to note, they are different urls can read here: https://webmasters.googleblog.com/2010/04/to-slash-or-not-to-slash.html but looks like you got a fix for your project 👍 |
Beta Was this translation helpful? Give feedback.
-
As i know, express exposes on both slash and without slash |
Beta Was this translation helpful? Give feedback.
trailing slash is considered a different URL so it's not a bug, you could check for trailing slash in your handler and remove it or redirect to non-trailing slash url: