【发布时间】:2020-04-28 09:30:37
【问题描述】:
我正在研究 NodeJS 和 React 中的应用程序,但我遇到了一个问题:
当我在 localhost 中使用我的应用程序时,我的长时间请求发布(20/30 分钟)的超时有效(写入 setupProxy.js 文件)。但是在部署在 Dokku 上的应用程序中,此超时不起作用(请求在 1 分钟后失败并出现错误:“POST 504 (Gateway Time-out)”)。我认为 Dokku 默认会超时,但我不确定,我也不知道如何修复它。
我的代码:
setupProxy.js
const { createProxyMiddleware } = require('http-proxy-middleware');
module.exports = function (app) {
var port;
if (process.env.PORT) {
port = process.env.PORT;
} else {
port = 5000;
}
app.use(
createProxyMiddleware('/api/students', {
target: `http://localhost:${port}`,
timeout: 3840000,
})
);
app.use(
createProxyMiddleware('/api', {
target: `http://localhost:${port}`,
})
);
};
server.js
const express = require('express');
const connectDB = require('./config/db');
const path = require('path');
const app = express();
//Connect Database
connectDB();
//Set the limit higher
app.use(express.json({ limit: '50mb' }));
app.use(express.urlencoded({ limit: '50mb', extended: true }));
//Define Routes
app.use('/api/users', require('./routes/api/users'));
app.use('/api/admins', require('./routes/api/admins'));
app.use('/api/auth', require('./routes/api/auth'));
app.use('/api/profile', require('./routes/api/profile'));
app.use('/api/students', require('./routes/api/students'));
app.use('/api/email', require('./routes/api/email'));
// Serve static assets in production
if (process.env.NODE_ENV === 'production') {
// Set static folder
app.use(express.static('client/build'));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}
//Look on the heroku app for the port OR take de default port 5000
const PORT = process.env.PORT || 5000;
var server = app.listen(PORT, () =>
console.log(`Server started on port ${PORT}`)
);
提前感谢您的帮助! 卡米尔。
【问题讨论】:
标签: node.js proxy timeout dokku