gpt4 book ai didi

mysql - 调用mysql后express-http-context丢失

转载 作者:行者123 更新时间:2023-11-29 09:55:10 26 4
gpt4 key购买 nike

需要你的帮助。我正在尝试为我的 Node.js 应用程序实现全局请求 ID。为此,我使用express-http-context lib来设置请求ID。但是当我的代码从 mysql 数据库访问数据时,它没有按预期工作。 express-http-contextmysql 库使用的 cls 似乎存在兼容性问题。

下面提供了示例代码和输出:

const app = require('express')();
const httpContext = require('express-http-context');
const mysql = require('mysql');
const { INTERNAL_SERVER_ERROR, OK, NOT_FOUND } = require('http-status-codes');
const http = require('http');
const uuidv4 = require('uuid/v4');

const pool = mysql.createPool({
host: '127.0.0.1',
port: 3306,
user: 'root',
password: 'Redhat@123',
database: 'user',
connectionLimit: 10,
});

pool.query('SELECT 1 + 1 AS solution', (error) => {
if (error) {
console.log('Unable to connect to MySQL:- ', error.message);
process.exit(1);
}
console.log('MySQL PING is Working');
});

app.use(httpContext.middleware);
app.use((req, res, next) => {
httpContext.set('requestId', uuidv4());
console.log('request Id set is: ', httpContext.get('requestId'));
next();
});

const getUserDetailsRepository = (userId, callback) => {
console.log('inside getuserDetails repository for request id: ', httpContext.get('requestId'));
pool.getConnection((connectionError, connection) => {
if (connectionError) {
console.log('got ConnError getuserDetails repository for request id: ', httpContext.get('requestId'));
callback(connectionError);
} else {
const query = 'SELECT * from user where id = ?';
connection.query(query, [userId], (queryError, results) => {
connection.release();
if (queryError) {
console.log('got queryError getuserDetails repository for request id: ', httpContext.get('requestId'), queryError.message);
callback(queryError);
} else {
console.log('Got response inside getuserDetails repository for request id: ', httpContext.get('requestId'));
callback(null, results);
}
});
}
});
};

const userGetDetails = (req, res, next) => {
const { userId } = req.params;
console.log('inside getUserDetails controller for request id: ', httpContext.get('requestId'));
getUserDetailsRepository(userId, (error, result) => {
if (error) {
console.log('got Error in getuserDetails controller for request id: ', httpContext.get('requestId'))
res.sendStatus(INTERNAL_SERVER_ERROR);
} else if (result) {
console.log('Got response inside getuserDetails repository for request id: ', httpContext.get('requestId'));
res.status(OK).json(result);
} else {
res.sendStatus(NOT_FOUND);
}
});
};

app.get('/user/:userId', userGetDetails);

const server = http.createServer(app);
server.listen(3000, () => console.log('Http server started listening on port'));

输出:

Http server started listening on port
MySQL PING is Working
request Id set is: ea4895ab-8003-4d28-99aa-b03af7027ae8
inside getUserDetails controller for request id: ea4895ab-8003-4d28-99aa-b03af7027ae8
inside getuserDetails repository for request id: ea4895ab-8003-4d28-99aa-b03af7027ae8
Got response inside getuserDetails repository for request id: undefined
Got response inside getuserDetails repository for request id: undefined

最佳答案

我希望这个解决方案能够解决您的问题,并为遇到此问题的任何人节省大量时间

你可以使用bindEmitter来解决这个问题

app.use((req, res, next) => {
httpContext.ns.bindEmitter(req);
httpContext.ns.bindEmitter(res);
var requestId = req.headers["x-request-id"] || uuidv4();
httpContext.set("requestId", requestId);
console.log('request Id set is: ', httpContext.get('requestId'));
next();
});

关于mysql - 调用mysql后express-http-context丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53976925/

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