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

オープン
#4,059 コメント 5 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@sarthak0906 がすでに取り組んでいます。

2020年3月7日 から。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
48/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
静か
技術スタック
express, javascript, node.js
領域
api, backend

調査の方向性

まず bug.js に示されている router と mount-path の挙動から始め、次に 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時間
マージ済み PR(30日)
4

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

expressjs/express のほかの issue

expressjs/express の issue をすべて見る

似ている issue

JavaScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。