Deke
Deke

Reputation: 4649

Not getting the value of query string in express

I'm trying to get the value of the query string term=+ e.target.value in the server but it's not showing up. Tried to write the route as '/doSomeSearch?term' and still no req.query.term doesn't show any value in the server. I changed the header from json to 'Content-Type': 'application/x-www-form-urlencoded' but only get 404 error back. Is the route not correct?

Input.js

handleInputBox (e) {
    if(event.keyCode == 13){
        event.preventDefault(); 
        fetch('http://localhost:3000/searchItems?term='+ e.target.value, {
          method: 'POST',
          headers:{
          'Content-Type': 'application/x-www-form-urlencoded'
        },
      },
        ).then(response => {
            if (response.ok) {
             response.json().then(json => {
               console.log("yes")
             });
            }else{
              console.log("no")
            }
          }
      ); 
   }
};

Server.js(Express)

app.get('/doSomeSearch?', function (req, res) {
   console.log(req.query)
})

Upvotes: 0

Views: 833

Answers (1)

Pranay Tripathi
Pranay Tripathi

Reputation: 1882

the routes should be POST method. As your are hitting a POST method type api and you have GET method configured, express cannot find the signature and return 404.

app.post('/doSomeSearch?', function (req, res) {
   console.log(req.query)
})

This should fix the issue. Hope it helps :)

Upvotes: 2

Related Questions