gpt4 book ai didi

javascript - ExpressJS 应用服务器在多次请求后返回 502

转载 作者:行者123 更新时间:2023-12-02 22:05:23 27 4
gpt4 key购买 nike

我是一名刚毕业的开发人员,也是 ExpressJS 的新学习者。我有一个用 ExpressJS 框架编写的应用程序服务器。网站向其发送几个 xhr 后,服务器开始返回 502 状态。

This is the server response

这是我启动应用程序服务器的代码:

let app = express();

app.use(cors());
app.set('view engine', 'pug');
app.set('views', './views');

app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));

app.use(express.static('public'));

app.use((req, res, next) => {
let id = guid();
req['id'] = id;
next();
})

app.use('/', (req, res, next) => {
for (let i in req.body) {
if (typeof req.body[i] === 'string')
req.body[i] = decodeURIComponent(req.body[i]);
}
next();
})

// Establish database object
app.use('/', async (req: Request, res: Response, next: NextFunction) => {
try {
req.db = new TDBHelper(pool);
next();
}
catch (e) {
next(e);
}
})

app.use('/file', express.static(__dirname + '/../bin/file'));

app.use('/api/v1', router);

app.use(async (req, res, next) => {
await req.db.end();
// Route not found.
const error = new Error();
error.message = 'API not found.';
error.status = 404;
next(error);
})

// On Error Handling
app.use(async (err, req: Request, res: Response, next: NextFunction) => {
await req.db.rollback();
console.log(err);
res.status(err.status || 500).json({
status_code: err.status_code || EStatusCode.FAIL,
message: err.message,
error: err.error
});
});

let server = app.listen(config.server.port, () => {
console.log(`Server is now running at port ${config.server.port}`);
});

我还使用 mysql 服务器。当服务器启动时,我创建了一个 mysql 池,每当有请求时,它就会从 mysql 池获取连接。每个请求结束后,我确实释放了池连接。我发现有时它会抛出有关“连接已释放”的错误。我不知道我是否正确处理mysql连接并导致这个问题。请帮忙,谢谢!

最佳答案

这个错误与express.js无关,当你的进程关闭时,Nginx会抛出这个错误,实际上发生的情况是,在某些情况下,即使池连接被释放,你也试图释放池连接。因此这会导致进程崩溃,并且 nginx 会在服务短暂关闭时发送 502 错误。

在代码中看到连接以 404 结束,但错误已传递到 错误处理中间件,如果发生错误,它会尝试回滚数据库更改。 因此,在回滚更改之前数据库连接已关闭。 你必须处理这部分。删除等待req.db.end();来自 404 处理程序并将其放置在 wait req.db.rollback() 之后;

app.use(async (req, res, next) => {
// await req.db.end(); // connection ends here on 404 but error is passed to
//error handling middleware where if error occur it tries to rollback the db //changes.
// So db connection is closed before rollbacking changes.
// You have to handle this part. remove this await req.db.end(); from here and //place it after await req.db.rollback();
// Route not found.
const error = new Error();
error.message = 'API not found.';
error.status = 404;
next(error);
})

// On Error Handling
app.use(async (err, req: Request, res: Response, next: NextFunction) => {
await req.db.rollback();
await req.db.end() // place it here
console.log(err);
res.status(err.status || 500).json({
status_code: err.status_code || EStatusCode.FAIL,
message: err.message,
error: err.error
});
});

关于javascript - ExpressJS 应用服务器在多次请求后返回 502,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59735516/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com