Rewriting req.url in router with prefix path results in missing slash (/)

Open
#4,059 5 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@sarthak0906 is already working on this.

Since Mar 7, 2020.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Quiet
Domain
api, backend

Research direction

Start with the router and mount-path behavior shown in bug.js, then run node bug.js and curl localhost:3000/app to reproduce the missing slash. Trace how express.Router and app.use('/app', router) remount the rewritten req.url; done means the logged path is /app/index.html.

Written by the indexing model from the issue text.

Description

4.x investigate module:router

Hello, I've been trying to use a npm module (connect-history-api-fallback) and encounter problems that after a lot of debugging I believe boils down to this issue.

When you're in a router that has a mount path, doing a rewrite on the relative root ('') path result in a missing slash, eg with bug.js as below

const express = require('express')
const app = express()
const port = 3000

var router = express.Router();
router.get('/', function (req, res, next) {
    req.url = "/index.html"
    next()
});

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

app.use(function (req, res, next) {
    console.log("Rewritten path: " + req.url)
    res.end()
});

app.listen(port, () => console.log(`Example app listening on port ${port}!`))

Running the following commands:

node bug.js
curl localhost:3000/app

results in the rewritten path to be /appindex.html instead of /app/index.html

Environment:

"node": 12.10.0
"express": 4.17.1

This sounds like a bug to me, but maybe I'm doing something wrong? I'm not too expert in expressjs to know for sure (my hunch is the router code doesn't 'remount' the prefix path properly.

Thanks!

Dominant language
JavaScript
Stars
69.7k
Forks
25.6k
Avg merge
2d 9h
Merged PRs (30d)
4

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from expressjs/express

All issues in expressjs/express

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.