gpt4 book ai didi

javascript - 将请求/响应存储到 Node JS Express 中的数据库表中

转载 作者:行者123 更新时间:2023-12-03 22:39:59 25 4
gpt4 key购买 nike

我正在努力
创建一个迷你 Wireshark/调试游乐场
我想存储通过我的 API 后端的所有 requestsresponses,以便我可以利用它们来调试正在发生的事情。
主要目标是在网页上生成日志表,并能够导出为 JSON 格式。
我有
一个用 Node JS 编写的 API,使用 Express 通过 Sequelize 连接到 Postgres 数据库
我有很多请求来自我的 API。
这是我的请求的示例

POST /api/login 
POST /api/getSessionTimeOut
POST /api/checkIfGroupExist/25050-telenet
POST /api/listUsersInGroup/25050-telenet
POST /api/primary/createVxLan/ingress/103
POST /api/primary/createVxLan/egress/103
POST /api/primary/createSwitch/103
POST /api/primary/createVxLan/ingress/104
POST /api/primary/createVxLan/egress/104
POST /api/primary/createSwitch/104
POST /api/backup/createVxLan/ingress/103
POST /api/backup/createVxLan/egress/103
POST /api/backup/createSwitch/103
POST /api/backup/createVxLan/ingress/104
POST /api/backup/createVxLan/egress/104
POST /api/backup/createSwitch/104
POST /api/primary/installDevice
POST /api/monitor/2724
...
POST /api/monitor/2724
POST /api/backup/installDevice
POST /api/monitor/2725
...
POST /api/monitor/2725
POST /api/createDynamicInterface/ingress/103
POST /api/createDynamicInterface/egress/103
POST /api/createDynamicInterface/ingress/104
POST /api/createDynamicInterface/egress/104
POST /api/createPolicyFirewall/v4/103/vpn
POST /api/createPolicyFirewall/v4/104/inline
POST /api/createPolicyFirewall/v4/103/inline
POST /api/createPolicyFirewall/v4/103/inline

POST /api/createPolicyFirewall/v6/103/vpn
POST /api/createPolicyFirewall/v6/103/inline
POST /api/createPolicyFirewall/v6/104/inline

POST /api/createPolicyFirewall/v6/103/inline

POST /api/installPackage/inline

POST /api/monitor/2726
...
POST /api/monitor/2726
POST /api/installPackage/vpn
POST /api/monitor/2727
...
POST /api/monitor/2727
我想将每个请求存储到我的数据库中的日志表中。
我试过了

移民
module.exports = {
up: (queryInterface, Sequelize) =>
queryInterface.sequelize.query('CREATE EXTENSION IF NOT EXISTS "uuid-ossp";')
.then(() => {
queryInterface.createTable('Logs', {
id: {
allowNull: false,
primaryKey: true,
type: Sequelize.Sequelize.UUID,
defaultValue: Sequelize.literal('uuid_generate_v4()')
},
user: {
type: Sequelize.STRING,
allowNull: true
},
accountId: {
type: Sequelize.STRING,
allowNull: true
},
cpeMac: {
type: Sequelize.STRING,
allowNull: false
},
pHnsId: {
type: Sequelize.STRING,
allowNull: true
},
gHnsId: {
type: Sequelize.STRING,
allowNull: true
},
serviceType: {
type: Sequelize.STRING,
allowNull: true
},
securityCluster: {
type: Sequelize.STRING,
allowNull: true
},
method: {
type: Sequelize.STRING,
allowNull: true
},
portalUrl: {
type: Sequelize.STRING,
allowNull: true
},
apiUrl: {
type: Sequelize.STRING,
allowNull: true
},
data: {
type: Sequelize.STRING,
allowNull: true
},
response: {
type: Sequelize.STRING,
allowNull: true
},
createdAt: {
type: Sequelize.DATE,
allowNull: false
},
updatedAt: {
type: Sequelize.DATE,
allowNull: false
},
deletedAt: {
type: Sequelize.DATE,
allowNull: true
}
})
}),
down: (queryInterface) => queryInterface.dropTable('Logs')
};

模型
module.exports = (sequelize, DataTypes) => {
const Log = sequelize.define('Log', {
user: {
type: DataTypes.STRING,
allowNull: true
},
accountId: {
type: DataTypes.STRING,
allowNull: true
},
cpeMac: {
type: DataTypes.STRING,
allowNull: false
},
pHnsId: {
type: DataTypes.STRING,
allowNull: true
},
gHnsId: {
type: DataTypes.STRING,
allowNull: true
},
serviceType: {
type: DataTypes.STRING,
allowNull: true
},
securityCluster: {
type: DataTypes.STRING,
allowNull: true
},
method: {
type: DataTypes.STRING,
allowNull: true
},
portalUrl: {
type: DataTypes.STRING,
allowNull: true
},
apiUrl: {
type: DataTypes.STRING,
allowNull: true
},
data: {
type: DataTypes.STRING,
allowNull: true
},
response: {
type: DataTypes.STRING,
allowNull: true
}
});

const schema = {
user: "user",
accountId: "accountId",
cpeMac: "cpeMac",
pHnsId: "pHnsId",
gHnsId: "gHnsId",
serviceType: "serviceType",
securityCluster: "securityCluster",
method: "method",
portalUrl: "portalUrl",
apiUrl: "apiUrl",
data: "data",
response: "response"
};

Log.list = (models) => new Transformer.List(models, schema).parse();
Log.single = (model) => new Transformer.Single(model, schema).parse();

return Log;
};

Controller
const Log = require('../models').Log;

module.exports = (config, jwtDecode, Op) => {
let logs = {};

/**
* Create a Log
*
* @return {object} log
*/
logs.create = async(req, res, next) => {
try {

let $body = {
name: log.name,
accountId: log.accountId,
cpeMac: log.cpeMac,
pHnsId: log.pHnsId,
gHnsId: log.gHnsId,
serviceType: log.serviceType,
securityCluster: log.securityCluster,
method: log.method,
portalUrl: log.portalUrl,
apiUrl: log.apiUrl,
data: log.data,
response: log.response
};

let response = await Log.create($body);
res.status(200).send(JSON.parse(response));
} catch (error) {
next(error);
}
};

return logs;
};

服务
module.exports = (config, request) => {
let log = {};

/*==============================
= create =
==============================*/

log.create = ($body) => {
let $options = {
method: "POST",
uri: `/api/logs/create`,
body: $body
};

return new Promise((resolve, reject) => {
request($options)
.then(data => resolve(JSON.stringify(data)))
.catch(error => reject(error));
});
};

return log;
};

路线
app.post('/api/logs/create', controllers.logs.create);

结果
现在,我已经准备好了所有的部分,但我不确定如何连接所有这些以便能够将所有请求/响应存储在数据库中?

最佳答案

作为将其创建为中间件的基本大纲,您将执行以下操作:

应用程序.js

/* You're imports and setup */

/*
any time a call is made, it will hit this function
app.use accepts a function, which will give the parameters
req, res, next.
i.e. app.use((req, res, next) => { ... })
so we'll pass in you're logs.create function
and the request will have all the information on what
endpoint is being given.
*/
app.use(controllers.logs.create)

/* the rest of your routes */

如果您提供 app.js 文件,我可以为您提供上述内容的更好版本

如果还有什么我可以做的,请告诉我。

关于javascript - 将请求/响应存储到 Node JS Express 中的数据库表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50221724/

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