创建一个迷你Wireshark / debuging playground
我想存储所有requests
& responses
通过我的API后端,因此我可以利用它们来调试正在进行的操作。
主要目标是在网页上生成日志表,并能够以JSON格式导出。
使用Express通过Sequelize连接到Postgres数据库的Node JS编写的API
我的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);
现在,我准备好了所有的部分。 ,但我不知道如何连接所有这些以便能够存储数据库中的所有请求/响应?
我希望你能有人给我一些提示。
如何进行并继续进行?
我现在对任何建议持开放态度。
任何提示/建议/帮助都将非常感谢!
答案 0 :(得分:0)
作为将其创建为中间件的基本概要,您可以这样做:
/* 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
文件,我可以为您提供上述内容的更好版本
让我知道我还能做些什么。