【发布时间】:2018-10-17 16:53:20
【问题描述】:
我在努力
创建一个迷你 Wireshark/调试游乐场
我想存储通过我的 API 后端传递的所有 requests 和 responses,以便我可以利用它们来调试正在发生的事情。
主要目标是在能够导出为 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;
};
控制器
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);
结果
现在,我已经准备好所有的部分,但我不确定如何连接所有这些以便能够将所有请求/响应存储在数据库中?
【问题讨论】:
-
如果你想记录你的 API 处理的请求,日志创建不需要是一个端点,将它作为中间件可能会更容易。然后对于每个请求,您可以调用您的 db 调用来制作该日志,您的
req具有所有标题/路径/等。您需要的信息。 -
@JoeLissner :感谢您的好心 cmets,我非常喜欢您的建议。由于我对所有这些 NodeJS 流程都有点陌生,请您留下更多步骤的答案,我将使用这些步骤进一步指导我。
-
我认为您需要的是可以在每次请求进入时执行此日志记录的钩子。您可以查看this
-
你需要做的就是记录日志。
标签: javascript node.js express logging sequelize.js