bbennett36 bbennett36 - 12 days ago 9
HTTP Question

How to pass GET form data to Express?

I have a form

<form action="./search" method="GET">
<div class="form-group text-center">
<input type="text" name="keyword" placeholder="Job Title" />
<button type="submit" class="btn btn-primary">Find Jobs</button>
</div>
</form>


If I enter "akron" in the form and submit and pass it to this next method it returns "Cannot GET /search?keyword=akron"

router.get('/search/:keyword', function(req, res) {
res.send('hello ' + req.params.keyword + '!');
})


But if I type http://localhost:3000/search/hello it will return the "hello akron!"

What is the correct way to pass the parameters?

Answer

Change to

action="/search"

The "./blah" syntax with dot in front is for files.