const express = require('express');
const request = require('request');
const bodyParser = require('body-parser');
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;
const data = {
members: [
{
email_address: email,
status: "subscribed",
merge_fields: {
FNAME: firstName,
LNAME: lastName
}
}
]
};
const jsonData = JSON.stringify(data);
const url ="https://us18.api.mailchimp.com/3.0/lists/081d03d860";
const options ={
method: "POST",
auth: "mick:2c775770a96a720b8c492df7974840d3-us18"
}
const apiRequest = https.request(url, options, function(response) {
if (response.statusCode === 200){
response.send('Successfully subscribed');
} else {
response.send('There was an error with signing up, please try again');
}
response.on('data', function(data){
console.log(JSON.parse(data));
});
});
apiRequest.write(jsonData);
apiRequest.end();
});
app.listen(3000, function(){
console.log('Server is running on port 3000');
});
I am working on a project using Node.js to create a newsletter sign up page that interacts with the MailChimp API. However, when I run my code in Node.js, I keep encountering the following error message. It appears that there is an issue with the event listener argument not being a function. Can someone assist me with resolving this problem?
TypeError: "listener" argument must be a function
at ClientRequest.once (events.js:340:11)
at new ClientRequest (_http_client.js:164:10)
at Object.request (http.js:38:10)
at Object.request (https.js:239:15)
at C:\Users\Desmond\Desktop\Web Development\Newsletter-Singup\app.js:40:24
at Layer.handle [as handle_request] (C:\Users\Desmond\Desktop\Web Development\Newsletter-Singup\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\Desmond\Desktop\Web Development\Newsletter-Singup\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\Desmond\Desktop\Web Development\Newsletter-Singup\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\Desmond\Desktop\Web Development\Newsletter-Singup\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\Desmond\Desktop\Web Development\Newsletter-Singup\node_modules\express\lib\router\index.js:281:22