简体   繁体   中英

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/akron it will return the "hello akron!"

What is the correct way to pass the parameters?

Change to

action="/search"

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

Also change to

router.get("/search"  //...

and use req.query

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM