expressjs / expressjs/express

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

Open
#4,059 5 comments 0 reactions 0 assignees View on GitHub
4.x investigate module:router
Dominant language
JavaScript
Stars
69.5k
Forks
25k
Avg merge
4d 20h
Merged PRs (30d)
9

Description

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
```js
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:
```js
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!

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.