title: How to wire up an Angular 2 Http.post call to Node.js backend and send NodeMailer Email
body: <p>I am brand new to using node.js on the backend and still wrapping my head around building a REST API and making calls on my front end that hit my backend. With that said, I am trying to get my Angular 2 app to send an email using nodemailer. If I route to /sendEmail, the email sends perfectly however I am trying to get the call to run when I click submit on my form and then pass an object with all my form data in it so I can send with nodemailer. Any help here would be greatly appreciated! Thanks!</p>
  
  <p>Ng2 Component HTML</p>
  
  <pre><code>&lt;div class="centered two-thirds"&gt;
     &lt;textarea name="project-description"&gt;&lt;/textarea&gt;
     &lt;input type="submit" (ngSubmit)="onSubmit()" name="submit" value="Submit" [disabled]=""class="tcenter centered"&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Ng2 Component</p>
  
  <pre><code>onSubmit() {
      this._wpService.sendEmail();
    }
  </code></pre>
  
  <p>Ng2 Service</p>
  
  <pre><code>sendEmail() {
      return this._http.post('/api/sendEmail', {name: 'Foo', email: 'foo@foo.com'})
      .map((response: Response) =&gt; response.json() )
      .toPromise()
      .catch(this.handleError);
    }
  </code></pre>
  
  <p>Server.js</p>
  
  <pre><code>// Get Dependencies
  const express = require('express');
  const path = require('path');
  const http = require('http');
  const bodyParser = require('body-parser');
  const nodemailer = require('nodemailer');
  
  // Get our API routes
  const api = require('./server/routes/api');
  
  const app = express();
  
  // Parsers for Post Data
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({ extended: false }));
  
  // Point static path to dist
  app.use(express.static(path.join(__dirname, 'dist')));
  
  // Set our api routes
  app.use('/api', api);
  
  // Catch all other routes and return the index file
  app.get('*', (req, res) =&gt; {
    res.sendFile(path.join(__dirname, 'dist/index.html'));
  });
  
  // Get Port from environment and store in express
  const port = process.env.PORT || '3000';
  app.set('port', port);
  
  const server = http.createServer(app);
  
  server.listen(port, () =&gt; console.log(`API running on localhost:${port}`));
  </code></pre>
  
  <p>/server/routes/api.js</p>
  
  <pre><code>const express = require('express');
  const router = express.Router();
  const nodemailer = require('nodemailer');
  
  /* GET api listing. */
  router.get('/', (req, res) =&gt; {
  
    res.send('api works');
  
  });
  
  
  router.post('/sendEmail', (req, res) =&gt; {
    console.log(res);
    console.log(req);
    var smtpConfig = {
        host: 'smtp.gmail.com',
        port: 465,
        secure: true,
        auth: {
            user: 'my-email@gmail.com',
            pass: 'my-pass'
        }
    };
  
    var transporter = nodemailer.createTransport(smtpConfig);
  
    var mailOptions = {
        from: 'email',
        to: 'email',
        subject: 'Test',
        text: 'test'
    };
  
    transporter.sendMail(mailOptions, function(error, info){
        if(error){
            return console.log(error);
        }
        console.log('Message sent: ' + info.response);
    });
  });
  
  module.exports = router;
  </code></pre>
  
  <p>Console Error</p>
  
  <p><code>POST http://localhost:3000/api/sendEmail 404 (Not Found)</code>
  <code>Response {_body: "Cannot POST /api/sendEmail↵", status: 404, ok: false, statusText: "Not Found", headers: Headers…}</code>
  <code>SyntaxError: Unexpected token C in JSON at position 0</code></p>