node.js - 简单的 POST 请求像 GET 请求一样工作并超时
问题描述
我创建了一个简单的 POST 请求表单来提交电子邮件,但每次我运行它时,它就像一个失败的 GET 请求。
我尝试在邮递员中对其进行测试,但我收到一条消息说“无法得到任何回应”。该表格非常基本,但我在网上找不到任何描述我的问题和有效解决方案的内容。
我已经在我的本地服务器、Heroku 服务器和 Godaddy 共享托管服务器上尝试过这个,它们的行为都是一样的。
我尝试添加不必要的斜杠,完全更改 URl,并删除除了 console.log 之外的所有内容。
app.post('/sendEmail', (req, res) => {
console.log('post test');
});
<form method="POST" action="/sendEmail">
<h1>Contact Me</h1>
<hr id="contact-page-hr">
<h4>I am here to serve YOU. Please send your questions my way.</h4>
<h6 id="contact-name-label"><span><input type="text" name="name" placeholder=" Name" value="" id="contact-name-input" required></span></h6>
<h6 id="contact-email-label"><span><input type="email" name="email" placeholder=" Email" id="contact-email-input" required></span></h6>
<h6 id="contact-message-label"></h6>
<textarea name="message" id="contact-message-input" placeholder=" What's on your mind?"></textarea>
<h6 class="sub-heading">(I usually reply within 48 hours)</h6>
<button type="submit" class="send-button" id="contact-page-send">Send</button>
</form>
我希望页面保持不变,但它却说:
“safari 无法打开页面 'localhost:5000/sendEmail/' 因为服务器意外断开连接...
它本质上就像一个 GET 请求
解决方案
您的请求处理程序不发送任何响应。因此,浏览器或服务器最终将超时等待响应的连接。
HTTP 请求需要某种响应。您需要res.send()
或res.redirect()
来自请求处理程序的东西。
仅当您使用 Ajax 调用发送 POST 请求时,该页面才会保持不变,但如果您执行非 Javascript 表单提交则不会。浏览器表单提交需要一个新的 HTML 页面或类似 302 重定向的东西作为其响应,并且客户端和服务器都在等待您的代码发送,直到其中一个最终超时并错误地关闭连接。
推荐阅读
- webpack - Webpack DefinePlugin 支持变量还是只支持常量?
- python - 如何附加到包含时间序列的 DataFrame 中的列
- python - 在模块导入时,打印出有用的语句
- select - 在 SQL Server 中根据列值选择记录
- javascript - 除日期外,如何在数字和非数字之间添加空格?
- protractor - protractor + cucumber 等待和超时而不执行完整测试
- php - 多个表的多个列连接到同一个表
- python - 我应该如何在非常长的、不连贯的序列上训练我的 Keras 有状态 LSTM?
- python - --noconsole 不适用于 Pyinstaller
- python - sklearn.pca() 和 n_components,线性代数困境