0

so I tried to get to stud_router.post(/students/add ) ,but I get directed to stud_router.get(/students/:email ) instead, I want to go /students/add but it sends me to /students/:email instead of /students/add.

stud_router.post(`/students/add` , (req,res,next) => {
    res.render(`studentadd/students_add`)  
})

stud_router.get(`/students/:email` , (req,res ,next) => {
        const found = data.some( stud => stud.email === req.params.email)
        if (found) {
            res.send(data.filter( stud => stud.email === req.params.email ))
        } else {
            res.status(400).send({ msg: `no member with the email of ${req.params.email}`})
        }

})
  • 1
    Define `/students/add` **first** – Phil Aug 17 '21 at 02:42
  • Does this answer your question? [Order of router precedence in express.js](https://stackoverflow.com/questions/32603818/order-of-router-precedence-in-express-js) – Phil Aug 17 '21 at 02:43
  • i've already tried it, but the order didn't work. – Abi Raditya Aug 17 '21 at 02:48
  • It would help greatly if you include some actual code in your question. See [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – Phil Aug 17 '21 at 02:49
  • 1
    One thing that does not make sense here is that a `.get()` and a `.post()` do not conflict as they never match the same request so order between them is not relevant. If `stud_router.get(/students/:email )` is matching, then the request MUST be a GET request, not a POST so perhaps you also have a problem with the type of request. – jfriend00 Aug 17 '21 at 02:51
  • 1
    oohhh i see, i think my understanding of get and post is wrong, thank you @jfriend00 i found my problem – Abi Raditya Aug 17 '21 at 02:55

1 Answers1

3

There's a problem in your question. These two routes:

stud_router.post(`/students/add`, ...)
stud_router.get(`/students/:email` , ...)

Do not overlap in any way because one is for a GET request and one is for a POST request. An incoming request will only match one of these requests. So, if something is going to stud_router.get('/students/:email' , ...), then it must be GET and it would never match the other route.

So, it appears that perhaps the client is confused about whether it's sending a POST or a GET. Or, your route definitions are confused about what they intend to match.

jfriend00
  • 683,504
  • 96
  • 985
  • 979