Home > Net >  Handling Multiple Parameters in the Same Path
Handling Multiple Parameters in the Same Path

Time:11-01

I am building an API with node and express that can encrypt and decrypt messages using common ciphers. In my API the standard path "format" is /<encrypt/decrypt>/<key(s)>. I am unsure currently on the best way to handel taking in multiple "key values" since some ciphers may take more than one key value for encryption.

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}`);
});

above is my current implementation. An example a path would be /affine/encrypt/hiddenmessage/1,25 which technically works fine but I feel as though this would not be the best way to implement what I am looking for. Is there a more efficient way for building this out?

CodePudding user response:

The values for this format can be sent in QueryParams. You are using plain express.js so, when you send data from the front-end/client, you can append that data in HTTP query params, and then these can be retrieved by:

const query = req.query;// query = {key:"abc", value: "123"}
  • Related