如何修复503(服务不可用)?

时间:2019-09-20 09:09:36

标签: node.js reactjs

我已成功在heroku上部署了reactApp,但是我的POST请求无法处理,而是在尝试提交联系表单时在控制台中收到错误消息?

我已经附上了控制台中记录的错误的屏幕截图

** package.json **在客户端文件夹中

{    “ proxy”:“ https://localhost:5000”, }

** 屏幕截图enter image description here ** // index.js

const express = require('express');
const bodyParser = require('body-parser');
const nodemailer = require('nodemailer');
const path = require('path');
const app = express();
// remove dotenv on deploy
//require('dotenv').config();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false}));

// Serve the static files from the React app
app.use(express.static(path.join(__dirname, 'client/build')));

// Handles any requests that don't match the ones above
app.get('*', (req, res) =>{
    res.sendFile(path.join(__dirname+'/client/build/index.html'));
});

app.post('/api/form', (req, res) => {
    // console.log(req.body)
    // console.log(process.env)

    nodemailer.createTestAccount((err, account) => {
        const htmlEmail = `
            <h4>Register as a software Engineer</h4>
            <ul>
                <li>First Name: ${req.body.name}</li>
                <li>Last Name: ${req.body.nam}</li>
                <li>Email: ${req.body.email}</li>
                <li>Years of Coding Experience: ${experience}</li>
            </ul>
        `
        // let testAccount = await nodemailer.createTestAccount();


        let transporter = nodemailer.createTransport({
           service: 'gmail',
            auth: {
              user: process.env.REACT_APP_FOO1,
              pass: process.env.REACT_APP_FOO3
            }
        });


        let mailOptions = {
            from: req.body.email,
            to: process.env.REACT_APP_FOO1,
            subject: req.body.name,
            text: req.body.email,
            html: htmlEmail
        }

        transporter.sendMail(mailOptions, (err, info) => {
            if (err) {
                return console.log('Message not sent !')
            }
            return console.log('Message sent successfully!' + info.body.email)
        })
    })
}) 

const PORT = process.env.PORT || 5000

app.listen(PORT, () => {
    console.log(`Server listening to port ${PORT}`)
})

1 个答案:

答案 0 :(得分:0)

此错误与您的服务器端代码有关。您的server.js发生错误。请添加一些代码。因此,我们可以更好地了解server.js中您在做什么。

问题:存在请求超时(应用需要30秒钟以上才能响应):code = H12 desc =“请求超时”状态= 503字节= 0

  

解决方案:需要30秒以上的代码必须在Heroku中异步运行(例如,作为后台作业)。有关更多信息,请阅读Heroku DevCenter中的“请求超时”。

    if(process.env.NODE_ENV === 'production'){
        const path  =  require('path');
        app.get('/*',(req,res)=>{
            res.sendfile(path.resolve(__dirname,'client','build','index.html'))
        })
    }

使用此插件

// Handles any requests that don't match the ones above
app.get('*', (req, res) =>{
    res.sendFile(path.join(__dirname+'/client/build/index.html'));
});