<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sagar Ghimire</title>
    <description>The latest articles on DEV Community by Sagar Ghimire (@gmrsagar).</description>
    <link>https://dev.to/gmrsagar</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F67999%2F25da34b2-0b0c-4d7c-97ee-a312cb5bef73.jpg</url>
      <title>DEV Community: Sagar Ghimire</title>
      <link>https://dev.to/gmrsagar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gmrsagar"/>
    <language>en</language>
    <item>
      <title>Setting up a Serverless Contact form in React - using Nodemailer and Express</title>
      <dc:creator>Sagar Ghimire</dc:creator>
      <pubDate>Sat, 26 Jan 2019 05:01:36 +0000</pubDate>
      <link>https://dev.to/gmrsagar/setting-up-a-serverless-contact-form-in-react-using-nodemailer-andexpress-4apl</link>
      <guid>https://dev.to/gmrsagar/setting-up-a-serverless-contact-form-in-react-using-nodemailer-andexpress-4apl</guid>
      <description>&lt;p&gt;Having a contact form always works better than just displaying an email address on our website. A contact form gives our visitors an easy way to get in touch with us. In this post, we will go through a simple way to set up our own in &lt;a href="https://reactjs.org"&gt;React&lt;/a&gt; using &lt;a href="https://nodemailer.com"&gt;Nodemailer&lt;/a&gt; and &lt;a href="https://expressjs.com"&gt;Express&lt;/a&gt; API. This post will also walk us through deploying it serverless with &lt;a href="https://zeit.co/now"&gt;Zeit&lt;/a&gt; Now.&lt;/p&gt;

&lt;h2&gt;
  
  
  What this app will do
&lt;/h2&gt;

&lt;p&gt;By the end of this tutorial, you will have a contact form on your website that will bring the message straight to your inbox.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/uWWmad9OgSckniKnEd/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/uWWmad9OgSckniKnEd/giphy.gif" alt="Gif from Giphy"&gt;&lt;/a&gt;&lt;/p&gt;
GIF from &lt;a href="https://giphy.com/"&gt;Giphy&lt;/a&gt;



&lt;h2&gt;
  
  
  Tools this app will use
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://github.com"&gt;GitHub&lt;/a&gt; — (for hosting code also required to deploy with Zeit later)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://npmjs.com"&gt;Npm&lt;/a&gt; — (for using JS packages like create-react-app)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://nodejs.org"&gt;Node JS&lt;/a&gt; and &lt;a href="https://expressjs.com"&gt;Express JS&lt;/a&gt; (as our API will be built in Express)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://reactjs.org"&gt;React JS&lt;/a&gt; (create-react-app for bootstrapping a standard React application and setting up our form)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/axios/axios"&gt;Axios&lt;/a&gt; (for submission of form data to our remote API)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://zeit.co/now"&gt;Zeit Now&lt;/a&gt; (to deploy our app serverless)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Steps
&lt;/h2&gt;

&lt;h2&gt;
  
  
  1.Getting Things Ready
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;GitHub Repos:&lt;/strong&gt; — We start by creating two &lt;a href="https://github.com"&gt;GitHub&lt;/a&gt; repos, one to host our React form and another for Node API. We could also do it in a single repo, but we are using two for better maintainability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Node &amp;amp; npm&lt;/strong&gt; — Download the latest version of node.js from the &lt;a href="https://nodejs.org/en"&gt;link&lt;/a&gt;. In this post we are using version 11.7.0. Node comes with npm. To make sure node and npm are installed, check their versions using the following commands on the terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//for node
node -v

//for npm
npm -v
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;React&lt;/strong&gt; — We’re using create-react-app for our React application where we will build our form. Install create-react-app globally and generate our React app using the following commands:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Install create-react-app globally
npm install create-react-app -g

//Generate a new react app called my-app
create-react-app my-app
cd my-app
npm start
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  2.The Form
&lt;/h2&gt;

&lt;p&gt;Let’s get started with setting up a component that returns an HTML form. We’re using &lt;a href="https://getbootstrap.com"&gt;Bootstrap&lt;/a&gt; classes for style in the code below, so they are optional You can use your own CSS classes as well.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Here, each input has an &lt;em&gt;onChange&lt;/em&gt; handler corresponding to a specific variable in our component’s state. So, when the input changes, the state gets updated as well. The form itself has an &lt;em&gt;onSubmit&lt;/em&gt; handler that calls the &lt;em&gt;formSubmit&lt;/em&gt; function which handles our API calls.&lt;/p&gt;

&lt;p&gt;We will have a look at the function, but before that, let’s install axios (which we will be using in the function) to make HTTP request to the API.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Install axios
npm install axios --save
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Setting up our component src/App.js:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Now, we add the functions to handle form submission in our component.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The &lt;em&gt;preventDefault()&lt;/em&gt; function (on line 2), as the name suggests, prevents the default action of the form, which would’ve caused a page reload. While the message is being sent, the button text changes to “…Sending”, and axios makes the API call.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note: “API_URI”(line 14) is the uri of the endpoint of our API which will be discussed in the deployment section of this post(Step 4).&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If everything goes right, the resetForm is called, which we haven’t defined yet. So let’s define the reset function that will reset our form fields and update our button label:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  3.API
&lt;/h2&gt;

&lt;p&gt;Now, we need something to transport our form inputs to the API, so we set up the nodemailer, config file and route. But before that, we need to setup &lt;a href="https://expressjs.com"&gt;Express.js&lt;/a&gt; in our Node API repo:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Initialize
npm init

//Install express and other dependencies
npm install express body-parser nodemailer cors --save
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now we need to make a small change in our package.json file. Find “scripts” and add the following “start” line to it:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
  "start": "node ."
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now, in our directory we create an index.js file with the following code:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Important Note:&lt;/strong&gt; Since we are working on the development version, the credentials will be exposed on GitHub, so make sure your repo is private or use a test email address for development version. Later in production, you can make use of the Zeit environment variables to store credentials safely.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We have installed body-parser to process the form data, and CORS to allow cross-origin requests.&lt;/p&gt;

&lt;p&gt;We have setup a Nodemailer SMTP Transport and our route that will receive the data from our React form and send an email to the destination email address that we specify.&lt;/p&gt;

&lt;h2&gt;
  
  
  4.Deploy
&lt;/h2&gt;

&lt;p&gt;Finally! with all that done it’s time to deploy our app. We’re using Zeit Now for deployment, you can choose any other service that you like.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/zLrMT1J1RuJ9u/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/zLrMT1J1RuJ9u/giphy.gif" alt="Dexter"&gt;&lt;/a&gt;&lt;/p&gt;
GIF from &lt;a href="https://giphy.com"&gt;Giphy&lt;/a&gt;



&lt;p&gt;Start by creating an account on &lt;a href="https://zeit.co"&gt;Zeit&lt;/a&gt;. Login and head over to “Now” from the top navigation. Connect your GitHub account and add the two repos that we just created. Now, we go back to our code and add an empty now.json file in both the repos.&lt;/p&gt;

&lt;p&gt;It’s now time to push our codes to the respective github repos.&lt;/p&gt;

&lt;p&gt;Now we create a new branch in both repos and work with those because we will need to create Pull Request for Zeit now to run deployment.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//create and change to new branch
git checkout -b new-branch
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now we add some content to the now.json files.&lt;br&gt;
For our React repo we will use the following content on our now.json:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;For our Node repo, we will use the following content on now.json:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;em&gt;Note: More examples for Zeit Now configurations can be found &lt;a href="https://github.com/zeit/now-examples"&gt;here&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Finally, we make a small change in our package.json in our React repo. &lt;br&gt;
Find “scripts” and add the “now-build” line to it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    "scripts": {
        ...
        "now-build": "react-scripts build &amp;amp;&amp;amp; mv build dist"
    }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, we push the codes to their respective Github repos(new-branch) and then create a Pull Request in each. Zeit Now will immediately deploy the app recognizing the now.json files. You can view the app URL by clicking “Show all checks” and then “details” in the Pull Request summary. Lastly, &lt;strong&gt;be sure to copy the link and replace API_URI in the App.js file in React app.&lt;/strong&gt; Now our form should successfully deliver emails.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thanks for reading!&lt;/em&gt;&lt;br&gt;
&lt;em&gt;Happy Coding!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>serverless</category>
      <category>javascript</category>
      <category>node</category>
      <category>contact</category>
    </item>
  </channel>
</rss>
