【问题标题】:Timeout in setupProxy works in localhost but not in dokku deployement applicationsetupProxy 中的超时在 localhost 中有效,但在 dokku 部署应用程序中无效
【发布时间】: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


    【解决方案1】:

    Dokku 使用 nginx 代理请求,我认为它的默认超时时间为 60 秒。

    看起来有一个插件可以用来在每个应用程序的基础上修改这个超时here。这也是直接在上游添加支持的不错选择。

    【讨论】:

    • 感谢您的回答,我试过了,但我没有权限在我的学校服务器上执行此操作。我会问我的老师。我希望它会起作用!
    • 随时让您的老师在 Dokku 项目中针对此上游发布功能请求,这绝对是一件好事:)
    猜你喜欢
    • 1970-01-01
    • 2020-10-07
    • 1970-01-01
    • 2020-12-25
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多