node.js - 处理同一路径中的多个参数
问题描述
我正在使用 node 和 express 构建一个 API,它可以使用通用密码加密和解密消息。在我的 API 中,标准路径“格式”是 /<encrypt/decrypt>/<key(s)>。我目前不确定处理多个“密钥值”的最佳方式,因为某些密码可能需要多个密钥值进行加密。
const express = require("express");
const app = express();
const port = 3000;
const checkInput = require("./input_validator");
//import affine cipher utilities
const [affine, reverseAffine, affineKeyValidator] = require("./ciphers/affine");
app.get("/affine/encrypt/:string/:key", (req, res) => {
if (checkInput(req.params.string)) {
let key = JSON.parse("[" + req.params.key + "]");
if (affineKeyValidator(key[0])) {
res.send({ text: affine(req.params.string, key[0], key[1]) });
}
}
});
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});
以上是我目前的实现。一个示例路径是 /affine/encrypt/hiddenmessage/1,25,它在技术上运行良好,但我觉得这似乎不是实现我正在寻找的最佳方式。有没有更有效的方法来构建它?
解决方案
此格式的值可以以QueryParams
. 您使用的是普通 express.js,因此,当您从前端/客户端发送数据时,您可以将该数据附加到 HTTP 查询参数中,然后可以通过以下方式检索这些数据:
const query = req.query;// query = {key:"abc", value: "123"}
推荐阅读
- go - JWT 认证策略
- compiler-construction - 在低级中间代码中计算数组和其他非标量的活跃度
- javascript - 如何根据javascript中的多个键对数组进行分组和转换?
- ios - 从字符串数组中查找最常见的子字符串
- python-3.x - 网页抓取,python,请求,下载 pdf 文件,身份验证
- qr-code - 对 QR 码的每个部分进行着色,例如计时模式和格式信息
- symfony - Symfony:如果他还没有使用 LDAP 身份验证的帐户,请注册用户
- swift - 如何访问 ParentViewController 的功能
- javascript - 电话号码的 Javascript 正则表达式
- c# - 从 Azure Web 应用程序连接到 On Prem SQL 服务器