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

trouble with routes #87

Closed
tigrazone opened this issue Feb 20, 2025 · 15 comments
Closed

trouble with routes #87

tigrazone opened this issue Feb 20, 2025 · 15 comments

Comments

@tigrazone
Copy link

I have this routes

const gallery = require('../controllers/gallery.controller');
const cacheMW = require('../utils/cacheMiddleware');
const translate = require('../utils/translates');
const responseUtils = require('../utils/response_utils');

const setGalleryPageFlag = (req, res, next) => {
  translate(res);
  res.locals.isGalleryPage = true;
  console.log('!!! setGalleryPageFlag');
  next();
};

module.exports = app => {
  router.get('/', setGalleryPageFlag, cacheMW.cache(cacheMW.CACHE_DURATION), gallery.indexFrontPage);

  router.get('/de/', responseUtils.changeLang(2), setGalleryPageFlag, cacheMW.cache(cacheMW.CACHE_DURATION), gallery.indexFrontPage);

  // gallery pages
  router.get('/gallery', setGalleryPageFlag, cacheMW.cache(cacheMW.CACHE_DURATION), gallery.index);

  router.get('/gallery/page/:page', setGalleryPageFlag, cacheMW.cache(cacheMW.CACHE_DURATION), gallery.index);

  router.get('/de/gallery', responseUtils.changeLang(2), setGalleryPageFlag, cacheMW.cache(cacheMW.CACHE_DURATION), gallery.index);

  router.get('/de/gallery/page/:page', responseUtils.changeLang(2), setGalleryPageFlag, cacheMW.cache(cacheMW.CACHE_DURATION), gallery.index);

  app.use('/', router);
};

with express works ok but with ultimate-express cant find
/gallery/page/4 for example and I have 404 on this route

@nigrosimone

This comment has been minimized.

@tigrazone
Copy link
Author

doesnt helps

@nigrosimone

This comment has been minimized.

@dimdenGD
Copy link
Owner

You didn't add router

@nigrosimone
Copy link
Contributor

You didn't add router

Mhh... also works...

const express = require("ultimate-express");

const app = express();

const router = express.Router();

router.get("/", (req, res) => res.status(200).send(""));

router.get("/de/", (req, res) => res.status(200).send(""));

router.get("/gallery", (req, res) => res.status(200).send(""));

router.get("/gallery/page/:page", (req, res) =>
  res.status(200).send(req.params.page)
);

router.get("/de/gallery", (req, res) => res.status(200).send(""));

router.get("/de/gallery/page/:page", (req, res) => res.status(200).send(""));

app.use('/', router);

app.listen(13333, () => {
  console.log("Server is running at http://localhost:13333");
});

@dimdenGD
Copy link
Owner

I can replicate it when I add a trailing slash at the end of request

@nigrosimone
Copy link
Contributor

I can replicate it when I add a trailing slash at the end of request

Yes! also to me

Cannot GET /gallery/page/4/

@nigrosimone
Copy link
Contributor

It seem uWebSockets behaviour uNetworking/uWebSockets#1049

@tigrazone
Copy link
Author

in my case with ultimate-express

/gallery/page/4/ 404
/gallery/page/4 works

how to solve it? just add one more router?
this variant doesnt works

router.get("/gallery/page/:page", (req, res) =>
  res.status(200).send(req.params.page)
);

router.get("/gallery/page/:page/", (req, res) =>
  res.status(200).send(req.params.page)
);

and this variant doesnt works ALSO!!!

router.get("/gallery/page/:page/", (req, res) =>
  res.status(200).send(req.params.page)
);

router.get("/gallery/page/:page", (req, res) =>
  res.status(200).send(req.params.page)
);

µExpress tries to optimize routing as much as possible
;-)

@nigrosimone
Copy link
Contributor

nigrosimone commented Feb 23, 2025

@dimdenGD a possible workaround is redirect all request to non-trailing slash url

#createRequestHandler() {
        this.uwsApp.any('/*', async (res, req) => {
            const { request, response } = this.handleRequest(res, req);

            // redirect to non-trailing slash url
            const host = req.getHeader('host')
            const path = req.getUrl()
            const query = req.getQuery()
            if(path.endsWith('/') && path != '/') return res.writeStatus('301').writeHeader('Location',`//${host}${path.slice(0,-1)}${query?'?'+query:''}`).end()

            const matchedRoute = await this._routeRequest(request, response);
            if(!matchedRoute && !response.headersSent && !response.aborted) {
                response.status(404);
                this._sendErrorPage(request, response, `Cannot ${request.method} ${request.path}`, false);
            }
        });
    }

@tigrazone
Copy link
Author

possible workaround is redirect all request to non-trailing slash url

Is must be configureble for some flags.
Because SEO masters have complicated requests sometimes. By default behaviour must be as express works

@nigrosimone
Copy link
Contributor

possible workaround is redirect all request to non-trailing slash url

Is must be configureble for some flags. Because SEO masters have complicated requests sometimes. By default behaviour must be as express works

Sure! Among other things, for SEO purposes, URLs with and without trailing slashes must be treated as two different URLs. If you want to serve the same content, one must perform a 301 redirect to the other.

@tigrazone
Copy link
Author

tigrazone commented Feb 23, 2025

I want just set flag for do it configureble. And I want to see it in documentation

@nigrosimone
Copy link
Contributor

I want just set flag for do it configureble. And I want to see it in documentation

this is my proposal #90

@dimdenGD
Copy link
Owner

Fixed in 1.3.18

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants