0

I did a small nodejs project using mailchimp. But when trying to send the data to server the error occurs. i am new to this and just now learning the course and i did everything exactly as the instructor did but this error occurs. Because it is video course it is not possible for me to clarify it and i can't clear solution are identify solution.

node:events:492
      throw er; // Unhandled 'error' event
      ^

Error: socket hang up
    at connResetException (node:internal/errors:720:14)
    at TLSSocket.socketOnEnd (node:_http_client:525:23)
    at TLSSocket.emit (node:events:526:35)
    at endReadableNT (node:internal/streams/readable:1359:12)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
Emitted 'error' event on ClientRequest instance at:
    at TLSSocket.socketOnEnd (node:_http_client:525:9)
    at TLSSocket.emit (node:events:526:35)
    at endReadableNT (node:internal/streams/readable:1359:12)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  code: 'ECONNRESET'
}
Node.js v18.17.0

This is nodejs code i tried and i don't know where the error is. the html is just from bootstrap.

const express = require("express");
const bodyParser = require ("body-parser");
const request = require ("request");

const https = require("https")
const app =express();

app.use(express.static("public"));
app.use(bodyParser.urlencoded({extended:true}));

app.get("/",function(req,res){

    res.sendFile(__dirname+"/signup.html");
});

app.post("/",function(req,res){

    const firstName = req.body.fName;
    const lastName =req.body.lName;
    const email = req.body.email;

    var data = {

     members:[{
        email_address:email,
        status:"subscribed",
        merge_fields:{
            FNAME:firstName,
            LNAME:lastName
        }

    }]};
    var jsonData =JSON.stringify(data);    

    const url = "https://us21.api.mailchimp.com/3.0/lists/bf6bb54d9b";
    const options = {
        method:"POST",
        auth:"ram:395b652282fb64615919e5ffbc62e569-us21"
    };
    const request = https.request(url, options, function(response){
        response.on("data",function(data){
            console.log(JSON.parse(data));
        });

        request.write(jsonData);
        req.end();
    });
});

app.listen(3000,function(req,res){
    console.log("Server is Running in Port 3000");
});

1 Answers1

0

The request.write(..) cannot be in the callback since the callback is called after the mailserver responds which means you did not send any data to the mailserver. Try bellow code in its own file test.js and run it with node test.js

const https = require("https");

var data = {
     members:[{
        email_address:email,
        status:"subscribed",
        merge_fields:{
            FNAME: 'firstname',
            LNAME: 'lastName'
        }
    }
]};

data = JSON.stringify(data);    

const url = "https://us21.api.mailchimp.com/3.0/lists/bf6bb54d9b";
const options = {
    method:"POST",
    auth:"...."
};

const request = https.request(url, options, function(response){
    response.on("data",function(data){
        console.log(JSON.parse(data));
        // you should respond to the request `app.post("/",function(req,res){` here
        // res.render or res.json or ...
    });
});

request.on('error', (e) => {
    console.error(e);
});

request.write(jsonData);
request.end();
Molda
  • 5,619
  • 2
  • 23
  • 39