Using Node.js to Send Emails via API

I've been grappling with an issue for over a week while trying to develop a web application that sends welcome emails to new subscribers. Despite my API code working perfectly, I cannot seem to get any output on the console indicating success or failure of the email delivery. No matter what I try, the terminal remains unresponsive.

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const axios = require('axios');

app.use(bodyParser.json());

app.get('/', (req, res) => {
  res.sendFile(__dirname + '/index.html');
});

// setting up a server to send welcome emails to users who subscribe

app.post('/send-welcome-email', async (req, res) => {
    const { email } = req.body;

    const emailData = {
        From: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e497838596858fa48081858f8d8aca818091ca8591">[email protected]</a>',
        To: email,
        Subject: 'Welcome to DevLink',
        HtmlBody: '<html><head></head><body><p>Welcome to DevLink, thanks for jooining our platform!</p></body></html>'
    };

    try {
        const response = await axios.post('https://api.postmarkapp.com/email', emailData, {
            headers: {
                'Authorization': `MY_API_TOKEN`,
                'Accept': 'application/json',
                'content-type': 'application/json'
            }
        });
        res.status(200).send('Email sent');
    } catch (error) {
        console.error(error);
        res.status(500).send('Something went wrong');
    }
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
body {
    font-family: Arial, sans-serif;
    background-color: #f0f0f0;
    margin: 0;
    padding: 0;
  }
  
  .container {
    max-width: 400px;
    margin: 0 auto;
    padding: 20px;
    background-color: #ffffff;
    box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    margin-top: 50px;
  }
  
  h1 {
    font-size: 24px;
    margin-bottom: 10px;
  }
  
  form {
    display: flex;
    flex-direction: column;
  }
  
  label {
    font-size: 16px;
    margin-bottom: 5px;
  }
  
  input[type="email"] {
    padding: 10px;
    margin-bottom: 10px;
    border: 1px solid #ccc;
    border-radius: 5px;
  }
  
  button {
    background-color: #007bff;
    color: #ffffff;
    border: none;
    padding: 10px;
    border-radius: 5px;
    cursor: pointer;
  }
  
  button:hover {
    background-color: #0056b3;
  }
  
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="style.css">
  <title>DevLink Subscription</title>
</head>
<body>
  <div class="container">
    <h1>Signup for our daily insider</h1>
    <form id="subscription-form">
      <label for="email">Enter your email:</label>
      <input type="email" id="email" name="email" required>
      <button type="submit">Subscribe</button>
    </form>
  </div>
  <script src="script.js"></script>
</body>
</html>

https://i.stack.imgur.com/zo6h9.png

https://i.stack.imgur.com/HepMM.png

Answer №1

Based on the screenshot you provided, it appears that you are attempting to send an HTTP request to your server using the curl command in the same terminal where your server is running with node.

However, since the terminal is currently occupied by the node process, it is unable to execute any new commands such as curl.

To resolve this issue, you should launch a separate terminal window to run the curl command.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Pass data between JavaScript and PHP using the Phaser framework

I am trying to pass a JavaScript variable to PHP and then store it in a database. Despite searching for solutions on Google, I have not been successful. Most suggestions involve using AJAX, but the code doesn't seem to work when I try it. I attempted ...

Posting data using an Ajax form submission without the need for page

After numerous attempts, I am still facing an issue where clicking the Submit button causes the page to refresh. What changes should I make to resolve this problem? <script> $('#submit').on('submit', function(event) { event.pre ...

Encountering an Empty Array Response in Postman While Utilizing Mongoose with Node.js

I am encountering an issue while trying to post data in my MongoAtlas database using Mongoose on Express and NodeJS. Postman is giving me back an empty array. Can anyone point out what I might be doing wrong? Database: mongodb+srv://username:password@clu ...

Leveraging Dust.js LinkedIn templates within a Node.js framework alongside Express 3.x

I am struggling to understand how to implement the dustjs-linkedin templates in express 3.x #app.js var dust = require('dustjs-linkedin'); app.set('view engine', 'dust'); app.get('/test1', routes.test1); # ...

Data binding functions properly only when utilizing the syntax within the select ng-options

In the book AngularJS in Action, I came across this Angular controller: angular.module('Angello.Storyboard') .controller('StoryboardCtrl', function() { var storyboard = this; storyboard.currentStory = null; ...

Streaming HTTP content on a domain secured with HTTPS using HTML5

Is it possible to stream HTTP on an HTTPS domain without triggering browser security errors? By default, the browser tends to block such requests. I rely on the hls.js library for desktop support with .m3u8 files. When I play content directly (on mobile o ...

Vue 3's "<Component :is="">" feature magically transforms camelCase into lowercase

Within my application, I have implemented a feature where users can customize the appearance of social media links on their page by defining which platforms they want to include. Each social media platform is represented by its own component responsible fo ...

Press the list button to reveal an enlarged box

Each item on this list contains a hidden box that should be shown after it is clicked. However, the issue arises when expanding one item causes other items to expand as well. Refer to the image for clarification: Image Link Is there a way to only expand ...

Move the internal array pointer forward to the next record in order to apply the insertAfter function within the jquery code

As a new jQuery user, I'm attempting to develop a jQuery function using data provided by PHP. My goal is to arrange DIV elements in order of their values using insertAfter method. However, I seem to be encountering some difficulty in advancing the poi ...

Exploring the concept of returning objects in jQuery

I'm really trying to grasp the inner workings of how jQuery creates the return object when searching for DOM elements. I've delved into the source code, but I must admit that it's not entirely clear to me yet. So, I'm reaching out here ...

How do I implement the use of "lengthMenu: [10, 25, 50]," within an if/else statement effectively?

Could someone help me with defining 2 different configuration lines for a datatable using if-else statements? I've tried writing the code but it doesn't seem to be working as expected. if(role === 1) { lengthMenu: [10, 25, 50], } else ...

Issue with child component not reflecting changes when the state is updated

In an attempt to pass a value to a child component, I am encountering an issue where the value does not update when the parent component's value changes. Below is my code: Child Component: class Test extends Component { constructor(props) { su ...

Problem with icon color not being applied to lightning-tab component in LWC

.html <lightning-tabset variant="vertical" > <lightning-tab class="class1" icon-name="utility:adduser" label="demo label1"> demo label1 </lightning-tab> <lightning-tab class=" ...

Utilizing variables upon the page being refreshed

Is there a way to retain certain data even when the page is refreshed? I have some values stored in an array and would like to keep them without losing them upon refreshing. ...

Achieving proper variable-string equality in Angular.js

In my Angular.js application, I am utilizing data from a GET Request as shown below. var app = angular.module('Saidas',[]); app.controller('Status', function($scope, $http, $interval) { $interval(function(){ ...

I utilized Bootstrap to validate the form, however, despite the validation, the form is still able to be submitted. What steps can I take to

I have implemented form validation using Bootstrap's 'needs-validation'. However, I am facing an issue where the form still gets submitted even when the validation fails. What I want is for the form submission to be prevented if the validati ...

Tedious.js Protocol Not Supported

I encountered the following issue while attempting to connect to a local SQL server database using Tedious. Despite trying both the IP address and localhost, I am unable to establish a connection. TCP/IP is enabled for MSSQLSERVER in the SQL Server Configu ...

Select the default option and what occurs in the event of an HTTP connection

I am currently facing an issue with two aspects. Firstly, I am struggling to set a default option in my Select element. Despite using ng-model, it only displays a blank option at the moment: <select class="form-control" ng-model="pageSize"> ...

Drop-down functionality in Bootstrap Form becomes unresponsive after screen width is decreased

I recently encountered a strange issue with my simple Bootstrap form. When I resize my browser width to mobile size or view it on a mobile device, the form stops functioning properly. Unfortunately, I am unable to provide any specific code examples at this ...

Compiling TypeScript to JavaScript with Intellij IDEA while preserving the folder hierarchy

Seeking assistance with maintaining directory structure when compiling Typescript to Javascript in Intellij Idea. The current directory setup is as follows: root - ts - SomeClass1.ts - SomeFolder - AwesomeClass2.ts - tsc The desired compiled file ...