为什么EC2/Node.js的GET请求可以工作,但POST甚至没有路由



我的问题的变体已经被问了几十次,但我尝试过的都不起作用。

对于GETPOST请求,我的Node.js API在本地主机上正常工作。

在我的EC2/nginx实例上,服务器块配置正确,并通过https提供静态索引。

然后,我将这个服务器块配置为API端口(8000(的代理,它还返回API正在侦听。

然后,我运行了一个简单的GET/索引端点,该端点经过路由并正常工作。

然而,类似的POST/checkEmail端点(记住,它在localhost上工作(在这里超时,并出现504错误。这就是它变得奇怪的地方。

它甚至不会console.log有效负载,即函数的第一行。这意味着它甚至没有正确路由。以下是我的路线:

const API = require("./controllers/api");
module.exports = [
{ method: 'POST', path: '/checkEmail', options: API.checkEmail },
{ method: 'POST', path: '/sendEmail', options: API.sendEmail },
{ method: 'POST', path: '/recaptcha', options: API.recaptcha },
{ method: 'GET', path: '/index', options: API.index }

]

既然GET请求返回了预期的响应,那么这意味着所有这些都是真的:

  • 防火墙规则允许流量通过443(HTTPS(端口
  • 该流量通过8000(API(端口进行代理
  • 服务器块正在正确地为文件提供服务
  • ssl证书有效
  • 请求被发送到上游服务器

终点:

const axios = require("axios");
const env   = require("dotenv").config();
const AWS = require("aws-sdk");
const subscriber = require("./middleware/subscriber");
const sanitizer  = require("./middleware/sanitizer");
exports.index = {
cors: {
origin: ["*"]
},
handler: (request, h) => {
return "API Fluente";
}
}
exports.checkEmail = {
cors: {
origin: ["*"]
},

handler: async (request, h) => {
// This is never logged:
console.log(request.payload);
const payload = request.payload;
const email = await sanitizer.email(payload.email);
if(!email) throw new Error("Invalid email");
const response = await verifyEmail(email);
console.log("checkEmail attempt:", email, response.data)
if (response.status === 200) {
return h.response(response.data).code(200);
}
}
}

我尝试过的:

  • 最小/完整服务器块配置设置
  • curl来自EC2 CLI的POST请求
  • 将端口从8000更改为其他端口(例如8003(
  • 增加超时时间没有意义,因为这些都是简单的请求,应该在很短的时间内返回响应

这些都没有任何区别。

发现问题!事实证明,它与AWS服务或Node无关。

我使用的是不推荐使用的hapi包,而我本应该使用@hapi/hapi。

最新更新