javascript - 为什么我从 Express 服务器收到 "404 cannot POST"错误?

标签 javascript node.js apache express

所以我有一个快速服务器,我用它来处理 webhook 更新。我让它在一个端口上运行,并且该端口在域的子页面 (domain.com/help/webhook) 上有一个 ProxyPass 的服务器配置。

<Location /help/webhook>
   ProxyPass  http://localhost:PORT/
   ProxyPassReverse http://localhost:PORT/
</Location>

我可以很好地获取根路由(它会发回带有一些文本的 200 响应)。但是当我尝试向其他路由 (domain.com/help/webhook/update) 发送 POST 时,我收到 404 cannot POST//update 错误。这是我的代码:

require('dotenv').config();
let child_process = require('child_process'),
user = process.env.GITHUB_USER,
port = process.env.WEBHOOK_PORT;

let app = require('express')();

app.get('/', (req, res) => {
        res.status(200).send('this is the cherwell app webhook endpoint. nothing to see here.');
});

app.post('/update', (req, res) => {
        let sender = req.body.sender,
        branch = req.body.ref;
        console.log('request received');
        if(branch.indexOf('master') > -1 && sender.login === user) {
                child_process.exec('./deploy.sh', (err, stdout, stderr) => {
                        if(err) {
                                console.error(err);
                                return res.send(500);
                        } else {
                                console.log('success');
                                res.send(200);
                        }
                });
        }
});

app.listen(port);

在 GitHub 中,设置 Webhook 并传递有效负载,这是响应:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot POST //update</pre>
</body>
</html>

知道这个问题可能是什么吗?我是根据在线 webhooks 的教程编写的,虽然这是我第一次部署应用程序,但看起来很简单,但我可能在服务器配置方面或在我的快速代码中遗漏了一些内容。我不知道是哪一个。

感谢您提前提供的帮助!

最佳答案

似乎发布到的 URL 是 //update 而不是 /update。您应该在位置参数中添加尾部斜杠。

来自 mod_proxy 的文档:

If the first argument ends with a trailing /, the second argument should also end with a trailing /, and vice versa. Otherwise, the resulting requests to the backend may miss some needed slashes and do not deliver the expected results.

https://httpd.apache.org/docs/2.4/mod/mod_proxy.html#proxypass

关于javascript - 为什么我从 Express 服务器收到 "404 cannot POST"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59847767/

相关文章:

javascript - 如何将此 JS 转换为 jQuery?需要添加淡入淡出效果

javascript - 尝试在不可扩展对象上定义属性时,WeakMap polyfill 抛出错误

node.js - 如何在 NodeJS 自定义二进制文件中包含其他模块?

javascript - 在 Javascript 中使用异步结果的最简洁方法

php - Apache 无法在 Mojave 上加载 PHP 扩展

apache - HTTP POST 中的各个字段是否有大小限制?

javascript - `before()` 和 `beforeEach()` 有什么区别?

javascript - 替换字符 OnKeyPress

node.js - 无法在 Windows 上提供 Angular 项目

apache solr 作为服务托管