繁体   English   中英

在 node.js 的生产服务器上获取 502 Bad Gateway

[英]Getting 502 Bad Gateway on the production server for node.js

我有这个电子邮件功能,它在我的 localhost:/3000 上工作正常,但在生产服务器上却没有。 更奇怪的是,即使在生产服务器上发送电子邮件也能正常工作,只要它是简单的电子邮件而不是带有附件版本的电子邮件。 我不知道生产中有什么不同导致它失败。 我收到错误消息:

502 错误网关 nginx/1.14.0 (Ubuntu)

该功能是过滤数据库并将查询输出通过电子邮件发送给用户。 我从哪里开始排除故障?

the file which works contact.handlebars

<form action="/emailfromcustomer" method="post">

    <input type="name" class="form-control" name="customerfirst" placeholder="First Name">
    <input type="submit" class="btn btn-dark" value="Send Inquiry"></input>
</form>

file which doesnt work email.handlebars

<form action="/email" method="post">
    <input id="date1" name="date1">
    <input type="submit" class="btn btn-success" value="Send the email"></input>
</form>



routes:
router.get('/contact', (req, res, next) => {

    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 >= @date1 '
    }

    var date1 = req.body.date1 || '2000-01-01'

    let promise = queries.queryTablewithPararams(thequery1, date1);
    promise.then(
      data => {
        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: `<b>Please find your report attached!</b>` // 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;
    }
}

#

嗨,如果您使用的是 nginx 服务器,那么在您的配置文件中,您必须为您的节点 api 设置代理。 在 /etc/nginx/sites-available/default.conf

server {
    //...some_stuff
    location /node_api {
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

并将路线用作:

router.get('/node_api/email', function(req, res) {
    //send_mail
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM