Home > front end >  html form submit for express req.params with Routes
html form submit for express req.params with Routes

Time:03-22

On the server I have something like,

app.get('/blog/:bloggerId/:Topic', (req, res) => {
  console.log(req.params.bloggerId);
  console.log(req.params.Topic);
});

But in the client side. form get method is using query opeator localhost:5500/Blog?BloggerId='{id}'&Topic='{topic}'

I need localhost:5500/Blog/{id}/{topic} How can I post request for the desired routing?

CodePudding user response:

You have two options

FIRST

change req.params by req.query

SECOND

you need to submit form manually using onsubmit event listener like this

document.getElementById('yourForm').addEventListener('submit',function(e){
    e.preventDefault();
    var bloggerId = document.getElementById('bloggerId').value;
    var topic = document.getElementById('topic').value;
    window.location.href=`localhost:5500/Blog/${bloggerId}/${topic}`;
});
  • Related