[英]How can I fix 503 (service unavailable)?
I deployed my reactApp with heroku successfully but my POST request can't be handled and instead am getting an error messages in the console when i try to submit the contact form?我使用 heroku 成功部署了我的 reactApp,但我的 POST 请求无法处理,而是在我尝试提交联系表单时在控制台中收到错误消息?
I have attached a screenshot for the errors logged in the console我附上了控制台中记录的错误的屏幕截图
** package.json **in the client's folder ** package.json ** 在客户的文件夹中
{ "proxy": " https://localhost : 5000", } {“代理”:“ https://本地主机:5000”,}
** screenshot enter image description here ** // index.js **截图在这里输入图片描述** // 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}`)
})
This error is related to your server-side code.somthing is going wrong in your server.js.please add some code.此错误与您的服务器端代码有关。您的 server.js 出现问题。请添加一些代码。 so, we get more idea what you are doing wrong in server.js.
因此,我们更了解您在 server.js 中做错了什么。
Problem: There's a request timeout (app takes more than 30 seconds to respond): code=H12 desc='Request timeout' status=503 bytes=0问题:有一个请求超时(app需要超过30秒才能响应):code=H12 desc='Request timeout' status=503 bytes=0
Solution: Code that requires more than 30 seconds must run asynchronously (eg, as a background job) in Heroku.
解决方案:需要超过 30 秒的代码必须在 Heroku 中异步运行(例如,作为后台作业)。 For more info read Request Timeout in the Heroku DevCenter.
有关详细信息,请阅读 Heroku 开发中心中的请求超时。
if(process.env.NODE_ENV === 'production'){ const path = require('path'); app.get('/*',(req,res)=>{ res.sendfile(path.resolve(__dirname,'client','build','index.html')) }) }
Use this insted of使用这个
// Handles any requests that don't match the ones above app.get('*', (req, res) =>{ res.sendFile(path.join(__dirname+'/client/build/index.html')); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.