Home > Software design >  how do I take (req.param ) through input & button instead of typing in URL bar
how do I take (req.param ) through input & button instead of typing in URL bar

Time:01-03

I am trying to take input from user and sending them to the URL which they typed in.

By concatenating it with url = "http://localhost:3000/" "/:userInput"

First I tried to take input from user and created a link using DOM method

<script>
function myFunction() {
  var x = document.getElementById("newLis").value;
  document.getElementById("btn").href ='/'   x;
 console.dir(x);
}
</script>

And tried to attach it to link href, but it didn't work.

Second, I tried to access the input value through post route and tried to directly send it in

app.get("/:pathName",(req,res)=>{
const pathName = _.capitalize(req.params.pathName);



//console.log(Array.isArray(pathName));
List.findOne({name : pathName}, (error, results)=>{

  if (!error){
    if(!results){
  //prompt("Hello! I am an alert box!");
     const list = new List({
     name : pathName,
     items : def
        })
     list.save();
     List.aggregate();
     res.redirect('/'   pathName);
  }else {
    res.render("list", {listTitle: results.name, newListItems: results.items});
      }
  }
});
});

But I don't know how it works.

CodePudding user response:

id of "newLis" and then set the href attribute of an element with an id of "btn" to the value of the user input with "/" appended to the beginning.

CodePudding user response:

In post request get the value of the URL typed by the user using req.body.name. Concatenate the strings to get the full path and then redirect the user to the path

app.post("/yourpath",(req,res)=>
{
  const path= "/" req.body.name;
  res.redirect("path");
}

Here name is value of name attribute in input tag.

  • Related