Express.js 应用在请求中添加了一个额外的斜杠 url

Express.js app adding an extra slash to the request url

我是 运行 Apache2 服务器上的 express.js 网络应用程序。现在,该应用程序非常基础,我要做的就是将请求 URL 打印到控制台。例如,当我在浏览器中访问 https://subdomain.example.com/node/something 时,我希望它将 /something 打印到控制台。相反,它打印 //something(注意开头的额外斜杠)。这意味着我编写的任何路由处理程序都将被丢弃。目前,我真的不知道是什么原因造成的。
可能相关的一点是我正在使用 Apache 的 mod_rewrite 删除 www.在 URL 开头,文件扩展名(.php 等)从末尾开始。我试过将它们注释掉以查看是否会改变它,但它没有。

我的应用程序:

const express = require('express');

const PORT = 8080;
const app = express();

app.use((req, res) => {
    console.log(req.url);
    res.status(200).send()
});

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

我的 Apache 虚拟主机配置:

<IfModule mod_ssl.c>
<VirtualHost *:443>

        Alias /fonts /usr/share/fonts/www

        <Directory "/fonts">
                Require all granted
        </Directory>

        <Directory "/errors">
                Require all denied
        </Directory>

        <Directory "/">
                DirectoryIndex login.php
        </Directory>

        ServerName admin.example.com
        ServerAlias www.admin.example.com

        DocumentRoot /var/www/admin.example.com
        ErrorDocument 403 /errors/nope.html
        ErrorDocument 404 /errors/nope.html

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
        LogLevel alert rewrite:trace6

        ProxyRequests Off
        ProxyPreserveHost On
        ProxyVia Full

        <Proxy *>
                Require all granted
        </Proxy>

        <Location /node>
                ProxyPass http://127.0.0.1:8080/
                ProxyPassReverse http://127.0.0.1:8080/
        </Location>

        RewriteEngine On

        RewriteCond %{HTTP_HOST} ^www\.admin\.example\.com$
        RewriteRule ^/(.*)$ http://admin.example.com/ [R=301,L]

        RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d
        RewriteRule ^(.+)/$  [R=301]

        RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME}\.php -f
        RewriteRule (.*) .php

        RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
        RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} -f
        RewriteCond %{REQUEST_URI} ^(.+)\.php$
        RewriteRule (.*)\.php$  [R=301,L]

        Include /etc/letsencrypt/options-ssl-apache.conf
        SSLCertificateFile /etc/letsencrypt/live/admin.example.com/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/admin.example.com/privkey.pem
</VirtualHost>
</IfModule>

如有任何帮助,我们将不胜感激。

更新: 我将 .js 文件移出了 /node 文件夹并移到了 Apache 站点的根目录中,并以某种方式解决了这个问题。但是,我仍然希望能够毫无问题地将它移动到文件夹中。

对 apache 不太好,但是如果你从末尾删除 / 会发生什么 RewriteRule ^/(.*)$ http://admin.example.com/ [R=301,L]

改为阅读 RewriteRule ^/(.*)$ http://admin.example.com [R=301,L]

Express 也喜欢在不需要时添加 / 所以我通过在不需要的地方删除它们来解决这个问题。

虽然对此持保留态度。就像我说的,我不太习惯 Apache,而且我只使用 express 大约一年,所以这两个都可能是完全错误的。

我的文件夹结构是这样的:

项目
---- index.php
----节点
-------- main.js

已通过更改虚拟主机配置中的此项修复:

<Location /node>
       ProxyPass http://127.0.0.1:8080/node
       ProxyPassReverse http://127.0.0.1:8080/node
</Location>