I am building an email address registration application using Node.js on Udemy, with mailchimp.
The instructor used heroku in Udemy, but now I am trying to host it using Render.com because heroku isn’t free.So I found a YouTube video. How to Deploy a Node.js App to Render.com for Free (Heroku Alternative) - YouTube
It was going well until about 5:50 in this video.
Here’s the code I typed in Hyper following the video.
rm -rf node_modules / package-lock.json
git init
git branch -M main
git add .
git commit -m "First Commit"
git remote add origin https://github.com/yoriss67/Newsletter-Signup.git git push -u origin main
node app.js
and below is app.js
const express = require('express');
const mailchimp = require("@mailchimp/mailchimp_marketing");
const https = require('node:https');
const { url } = require('node:inspector');
const app = express();
app.use(express.static('public'))
// old 「body-parser」
app.use(express.urlencoded({
extended: true
}));
app.get('/', function (req, res) {
res.sendFile(__dirname + '/signup.html')
})
// -------------
mailchimp.setConfig({
apiKey: "eeb85b047aa8497f6038c75a82978ab2-us◯◯",
server: "us◯◯"
});
// --------------
app.post('/', function (req, res) {
const firstName = req.body.fName;
const lastName = req.body.lName;
const email = req.body.email;
console.log(firstName, lastName, email)
//*****************************ENTER YOU LIST ID HERE******************************
const listId = "◯◯";
//Creating an object with the users data
const subscribingUser = {
firstName: firstName,
lastName: lastName,
email: email
};
// *** Construct Requesting data ***
// members's key-value pair
const data = {
members: [{
email_address: email,
status: 'subscribed',
merge_fields: {
FNAME: firstName,
LNAME: lastName
}
}]
}
// 👩🎓Now we have our data object completed, but this is JavaScript and what we need is actually to turn this into a flatpack JSON.
const jsonData = JSON.stringify(data);
// 👩🎓make our request
// const url = "https://us{list server number}.api.mailchimp.com/3.0/lists/{List ID}"
const url = "https://us◯◯.api.mailchimp.com/3.0/lists/◯◯"
const options = {
method: 'POST',
// how we use API key for auth? > A. HTTP Basic Authentication (a pair of ①string as username ②API key)
auth: 'yoriss67:eeb85b047aa8497f6038c75a82978ab2-us◯◯'
}
// 🤔constにしないとその場で終わっちゃってdataをserverに持っていって活用できなくなるから?
const request = https.request(url, options, function(response) {
if(response.statusCode === 200) {
// res.send('Successfully subscribed!!!!!!')
res.sendFile(__dirname + '/success.html')
} else {
// res.send('There was an error')
res.sendFile(__dirname + '/failure.html')
}
response.on('data', function(data) {
console.log(JSON.parse(data))
})
})
request.write(jsonData);
request.end();
})
// 250
app.post('/again', function(req, res) {
console.log('Again')
res.sendFile(__dirname + '/signup.html')
})
app.listen(2480, function () {
console.log('server is running on port 2480')
});
package.json
{
"name": "newsletter-signup",
"version": "1.0.0",
"description": "Newsletter-Signup project!",
"main": "app.js",
"scripts": {
"start": "node app.js",
"test": "echo \"Error: no test specified\" && exit 1",
"build": "nodemon app.js"
},
"author": "yoriss67",
"license": "ISC",
"dependencies": {
"@mailchimp/mailchimp_marketing": "^3.0.80",
"async": "^3.2.4",
"express": "^4.18.2",
"https": "^1.0.0",
"request": "^2.88.2",
"yarn": "^1.22.19"
}
}
Of course I try to do my best but I hope someone tells me how to figure out this problem.