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

未关闭
#4,059 5 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@sarthak0906 已经在做这个了。

开始于 2020年3月7日。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
48/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
冷清
领域
api, backend

调研方向

先从 bug.js 中展示的 router 和挂载路径行为开始,然后运行 node bug.js 和 curl localhost:3000/app,以复现缺少斜杠的问题。跟踪 express.Router 和 app.use('/app', router) 如何重新挂载重写后的 req.url;当记录的路径为 /app/index.html 时即完成。

由索引模型根据 Issue 内容生成。

描述

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!

主要语言
JavaScript
星标
69.7k
派生
25.6k
平均合并
2 天 9 小时
30 天内合并 PR
4

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

expressjs/express 的其他 Issue

查看 expressjs/express 的全部 Issue

相似的 Issue

更多 JavaScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。