【发布时间】:2022-08-03 13:55:23
【问题描述】:
我想在 mailchimp API 的帮助下创建一个时事通讯。在下面你可以找到我为注册而编写的必要代码。
当我填写表格并按提交时出现错误。
signup.html 文件包含一个简单的表单,其中包含 3 个输入(名字、姓氏和电子邮件)和一个提交按钮。
这是我的 app.js 代码:
const express = require(\"express\");
const request = require(\"request\");
const bodyParser = require(\"body-parser\");
const app = express();
const https = require(\"https\");
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(\"public\"));
app.get(\"/\", function (req, res) {
res.sendFile(__dirname + \"/signup.html\");
});
app.post(\"/\", function (req, res) {
const fname = req.body.firstName;
const sname = req.body.lastName;
const e_mail = req.body.Email;
const data = {
members: [
{
email_address: e_mail,
status: \"subscribed\",
merge_fields: [
{
FNAME: fname,
LNAME: sname,
},
],
},
],
};
var jsonData = JSON.stringify(data);
const url = \"https://us10.api.mailchimp.com/3.0/lists/{list_id}\";
const options = {
method: \"POST\",
auth: \"hamza:{API_id}\",
};
https.request(url, options, function (response) {
const request = response.on(\"data\", function (data) {
console.log(JSON.parse(data));
});
request.write(jsonData);
request.end();
});
});
app.listen(3000, function () {
console.log(\"working successfully\");
});
node:events:505
throw er; // Unhandled \'error\' event
^
Error: socket hang up
at connResetException (node:internal/errors:692:14)
at TLSSocket.socketOnEnd (node:_http_client:478:23)
at TLSSocket.emit (node:events:539:35)
at endReadableNT (node:internal/streams/readable:1345:12)
at processTicksAndRejections (node:internal/process/task_queues:83:21)
Emitted \'error\' event on ClientRequest instance at:
at TLSSocket.socketOnEnd (node:_http_client:478:9)
at TLSSocket.emit (node:events:539:35)
at endReadableNT (node:internal/streams/readable:1345:12)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
code: \'ECONNRESET\'
}
[nodemon] app crashed - waiting for file changes before starting...