Express API working locally but not in amplify

Viewed 30

I am new to amplify and I have a simple project where I send email from my backend with nodemailer and that email to a mailchimp list and everything was working fine locally but when I hosted it with aws amplify CLI. it showed it was successful but nothing actually happened

here is my code:

const express = require("express")
const EmailSender = require("./emailSender.js")
const client = require("mailchimp-marketing")
const bodyParser  = require("body-parser")
const awsServerlessExpressMiddleware = require('aws-serverless-express/middleware')

// declare a new express app
const app = express()
app.use(bodyParser.json())
app.use(awsServerlessExpressMiddleware.eventContext())

// Enable CORS for all methods
app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*")
  res.header("Access-Control-Allow-Headers", "*")
  next()
});


/**********************
 * Example get method *
 **********************/

app.get('/add', function(req, res) {
  // Add your code here
  console.log(req.body)
  
});

app.get('/add/*', function(req, res) {
  // Add your code here
  res.json({success: 'get call succeed!', url: req.url});
});

/****************************
* Example post method *
****************************/

app.post('/add', function(req, res) {
  // Add your code here
  try {
    const { firstName, lastName,email,budget, message,company,country} = req.body
    EmailSender({firstName, lastName,email,budget, message,company,country})
    res.json({ msg: `Your message sent successfully`  });
  } 
  catch (error) {
    res.status(404).json({ msg: "Error ❌" });
  }
  const client = require("mailchimp-marketing");

client.setConfig({
apiKey: "xxxx" ,
server: "xxx",
});


 
  const run = async () => {
    const response = await client.lists.addListMember("xxxxxx", {
      email_address: req.body.email ,
      merge_fields: {
          FNAME: req.body.firstName,
          LNAME: req.body.lastName
      } ,
      status: "subscribed",
    });
    console.log(response);
    };
    run().catch(errors => console.log(errors.response.text));
   
    
    

//  catch(err) {
//   console.log("erorr")
//  }


  console.log(req.body)
});

app.post('/add/*', function(req, res) {
  // Add your code here
  res.json({success: 'post call succeed!', url: req.url, body: req.body})
});

/****************************
* Example put method *
****************************/

app.put('/add', function(req, res) {
  // Add your code here
  res.json({success: 'put call succeed!', url: req.url, body: req.body})
});

app.put('/add/*', function(req, res) {
  // Add your code here
  res.json({success: 'put call succeed!', url: req.url, body: req.body})
});

/****************************
* Example delete method *
****************************/

app.delete('/add', function(req, res) {
  // Add your code here
  res.json({success: 'delete call succeed!', url: req.url});
});

app.delete('/add/*', function(req, res) {
  // Add your code here
  res.json({success: 'delete call succeed!', url: req.url});
});

app.listen(3000, function() {
    console.log("App started")
});

// Export the app object. When executing the application local this does nothing. However,
// to port it to AWS Lambda we will create a wrapper around that will load the app from
// this file
module.exports = app
 

Please help

here is my cloudwatch console

enter image description here

Emailsender.js


 const EmailSender = ({ firstName, lastName,email,budget, message,company,country }) => {

  const sgMail = require('@sendgrid/mail')
sgMail.setApiKey("API_KEY")

const msg = {
  to: 'email@gmail.com', 
  from: 'info@example.io', 
  subject: 'Sending with SendGrid is Fun',
  text: 'and easy to do anywhere, even with Node.js',
  html: `
  <div>${firstName}<div/>
  <div>${lastName}<div/>
  <div>${email}<div/>
  <div>${budget}<div/>
  <div>${message}<div/>
  <div>${company}<div/>
  <div>${country}<div/>
  ` ,
}

sgMail
  .send(msg)
  .then((response) => {
    console.log(response[0].statusCode)
    console.log(response[0].headers)
  })
  .catch((error) => {
    console.error(error)
  })
0 Answers
Related