Search code examples
htmlnode.jsformsejsrouteparams

EJS Form GET Request with a search bar not working as expected


I am trying make a static search bar that currently is redirected to '/'. That is just the current situation. But the main problem is that whatever the input is, the output is always 'h'

I am using nodejs and ejs

.

request.ejs

      <form action="/request/search" method="GET">
            <input
              type="text"
              name="search"
              id="search"
              placeholder="Search for Books"
              class="search"
            />
            <button class="search-btn" type="submit">Submit</button>
        <div class="imae"></div>
      </form>

server.js


app.get('/request/search?:search', (req, res) => {
  console.log(req.params);
  res.redirect('/');
});

output

{ search: 'h' }

Am I Doing something wrong, Please Help


Solution

  • You didn't specify, but it appears you're using express.

    Use the req.query object rather than req.params, as params are used for URLs with routes, not query parameters. With that being said, you do not need the :search in your request URL match. I can't explain why it's receiving a value of 'h', but this should solve your problem.