在传出请求express js中使用中间件
Use middleware in outgoing request express js
我在网络中有一些快速服务器。我通过 HTTP 请求和使用特定的消息类型相互通信。常见的请求结构是这样的,
{
nonce: (randomNumber),
type: messageType,
message: {
}
}
我用加密算法 A、B 和 C(取决于消息类型)加密 "message"(不是 "nonce" 或 "type")。我了解到我可以使用 express 中间件来解密传入的加密请求。我如何使用中间件(或任何其他可能的解决方案)根据消息类型自动加密传出请求?
传出请求没有中间件。
作为一种可能的解决方案 - 您可以包装请求函数并要求它而不是请求模块。
例如:
加密-request.js:
const fetch = require("node-fetch"); // used promise version of request lib
module.exports = (options) => {
// encryption logic ...
return fetch(options);
}
一些-controller.js:
const fetch = require("./encrypted-fetch.js");
//just use as usual fetch somewhere
我在网络中有一些快速服务器。我通过 HTTP 请求和使用特定的消息类型相互通信。常见的请求结构是这样的,
{
nonce: (randomNumber),
type: messageType,
message: {
}
}
我用加密算法 A、B 和 C(取决于消息类型)加密 "message"(不是 "nonce" 或 "type")。我了解到我可以使用 express 中间件来解密传入的加密请求。我如何使用中间件(或任何其他可能的解决方案)根据消息类型自动加密传出请求?
传出请求没有中间件。
作为一种可能的解决方案 - 您可以包装请求函数并要求它而不是请求模块。
例如:
加密-request.js:
const fetch = require("node-fetch"); // used promise version of request lib
module.exports = (options) => {
// encryption logic ...
return fetch(options);
}
一些-controller.js:
const fetch = require("./encrypted-fetch.js");
//just use as usual fetch somewhere