title: Getting 502 Bad Gateway on the production server for node.js
body: <p>I have this email feature which works fine on my localhost:/3000 but not on the production server. What is stranger, it works fine even on the production server to email as long as it's the simple email and not the email with attachment version. I can't tell what's different in production that's causing it to fail. I'm getting the error message: </p>
  
  <blockquote>
    <p>502 Bad Gateway nginx/1.14.0 (Ubuntu)</p>
  </blockquote>
  
  <p>The feature is to filter a database and email the query output to the user. Where do I even begin to troubleshoot?</p>
  
  <pre><code>the file which works contact.handlebars
  
  &lt;form action="/emailfromcustomer" method="post"&gt;
  
      &lt;input type="name" class="form-control" name="customerfirst" placeholder="First Name"&gt;
      &lt;input type="submit" class="btn btn-dark" value="Send Inquiry"&gt;&lt;/input&gt;
  &lt;/form&gt;
  
  file which doesnt work email.handlebars
  
  &lt;form action="/email" method="post"&gt;
      &lt;input id="date1" name="date1"&gt;
      &lt;input type="submit" class="btn btn-success" value="Send the email"&gt;&lt;/input&gt;
  &lt;/form&gt;
  
  
  
  routes:
  router.get('/contact', (req, res, next) =&gt; {
  
      res.render('contact');
  
  });
  
  
  router.get('/email', function(req, res) {
  
    res.render('email')
  
  });
  
  
  
  
  router.post('/emailfromcustomer', function(req, res) {
  
  
    var customerfirst =  req.body.customerfirst || 'NoFirstNmae'
  
    const nodemailer = require("nodemailer");
    main().catch(console.error);
  
    // async..await is not allowed in global scope, must use a wrapper
    async function main() {
  
      // Generate test SMTP service account from ethereal.email
      // Only needed if you don't have a real mail account for testing
      let testAccount = await nodemailer.createTestAccount();
  
      let transporter = nodemailer.createTransport({
        host: "myhost.com",
        port: 465,
        /*587 25*/
        secure: true, // true for 465, false for other ports
        auth: {
          user: 'theuser',
          pass: 'somepass' 
        }
      });
  
  
      let info = await transporter.sendMail({
        from: 'someemail@gmail.com', 
        to: 'myemailgmail.com', // list of receivers
        subject: "You got mail", // Subject line
        text: "Customer " + customerfirst
      });
  
      res.send('Your message has been sent!')
    }
  
  });
  
  
  
  router.post('/email', function(req, res) {
  
    sendEmail();
  
    function sendEmail() {
      var thequery1 = `select top 4 product,units_sold,manufacturing_price 
      FROM table
        where 1 = 1 `
      if (req.body.date1) {
        thequery1 = thequery1 + ' and  Date &gt;= @date1 '
      }
  
      var date1 = req.body.date1 || '2000-01-01'
  
      let promise = queries.queryTablewithPararams(thequery1, date1);
      promise.then(
        data =&gt; {
          var csv = [];
          const fields = ['product', 'units_sold', 'manufacturing_price'];
          const json2csvParser = new Json2csvParser({
            fields
          });
          csv = json2csvParser.parse(data);
          var path = './public/serverfiledownload/file' + Date.now() + '.csv';
          fs.writeFile(path, csv, function(err, data) {
            if (err) {
              throw err;
            } else {
  
              main(csv, path).catch(console.error);
              res.send('it sent email')
            }
          });
        }
      );
    }
  
    const nodemailer = require("nodemailer");
  
    // async..await is not allowed in global scope, must use a wrapper
    async function main(csv, path) {
  
      // Generate test SMTP service account from ethereal.email
      // Only needed if you don't have a real mail account for testing
      let testAccount = await nodemailer.createTestAccount();
  
  
  
      let transporter = nodemailer.createTransport({
        host: "myhost.com",
        port: 465,
        /*587 25*/
        secure: true, // true for 465, false for other ports
        auth: {
          user: 'theuser',
          pass: 'somepass' //  ''generated ethereal password
        }
      });
  
      let info = await transporter.sendMail({
        from: 'someemail@gmail.com', // sender address
        to: req.body.inputemail, // list of receivers
        subject: "dynamic attachment", // Subject line
        text: "Hello world?", // plain text body
        attachments: [{ // utf-8 string as an attachment
          filename: path,
          content: csv
        }],
        html: `&lt;b&gt;Please find your report attached!&lt;/b&gt;` // html body
      });
  
      console.log("Message sent: %s", info.messageId);
  
      // Preview only available when sending through an Ethereal account
      console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
    }
  
  });
  
  
  /etc/nginx/conf.d/server.conf
  #
  
  server {
      listen 80;
  
      server_name example.com;
  
  
      location / {
      proxy_pass http://localhost:3000;
      }
  }
  
  #
  </code></pre>