Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

81
Views
NodeJS Express: I have two app.get requests and they blend together

So I have 1 request that is

app.get('/assignment/loans', (req, res) => { 
    const id = req.query.bookID;
}

and another that is this:

app.get('/assignment/loans', (req, res) => {
    const id = req.query.studentID;
}

For some reason, the studentID one does not work and it always goes to the bookID to search. I use this

http://localhost:3000/assignment/loans?bookID=1

and it works as intended but if I use

http://localhost:3000/assignment/loans?studentID=9653

I get:

enter image description here

which is an error that should only appear for the bookID. How can I differentiate the two? Thank you in advance.

More code regarding the two requests: https://pastebin.com/1A0jK3BX

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You cannot have 2 routes with same url. first one take precedence. You need to handle it like this.

app.get('/assignment/loans', (req, res) => { 
   const bookID = req.query.bookID;
   const studentID = req.query.studentID;
   if(bookID){
   }
   if(studentID){
   }

}
about 4 years ago · Juan Pablo Isaza Report

0

actually express framework will execute the first endpoint when request happened which is

app.get('/assignment/loans', (req, res) => { const id = req.query.bookID;})

in duplicated endpoint case, so you should using one route and put tow queries instead of one so the code will be like this

app.get('/assignment/loans', (req, res) => { 
const book_id = req.query.bookID;
const student_id = req.query.studentID
})
  

also you have to make validation for queries before executed in db

about 4 years ago · Juan Pablo Isaza Report

0

You have exact same routes

app.get('/assignment/loans', ...) it has to be one route.

Try combining em like this below

app.get('/assignment/loans', (req, res) => {
  const bookId = req.query.bookID;
  const studentId = req.query.studentID;
  const loans = [];

  if(bookId){
    if(!Number.isInteger(parseInt(studentId))){
      return res.status(422)
        .setHeader('content-type', 'application/json')
        .send({error: 'bookId not numeric'});
    }
    db.all('SELECT * FROM loan WHERE bookID=?', bookId, (err, rows) => {
      if (err) {
        res.status(422)
          .setHeader('content-type', 'application/json')
          .send({error: 'Problem while querying database'});
        return;
      }
      if (rows.length === 0) {
        res.status(404)
          .setHeader('content-type', 'application/json')
          .send({error: 'loan bookId was not found!'});
      } else {
        rows.forEach(row =>
          loans.push({id: `${row.id}`, bookID: `${row.bookID}`}));
        res.status(200)
          .setHeader('content-type', 'application/json')
          .send(loans);
      }
    });
  }else if(studentId){
    if(!Number.isInteger(parseInt(studentId))){
      return res.status(422)
        .setHeader('content-type', 'application/json')
        .send({error: 'studentId not numeric'});
    }
    db.all('SELECT * FROM loan WHERE studentID=?', studentId, (err, rows) => {
      if (err) {
        return res.status(422)
          .setHeader('content-type', 'application/json')
          .send({error: 'Problem while querying database'});
      }
      if (rows.length === 0) {
        res.status(404)
          .setHeader('content-type', 'application/json')
          .send({error: 'loan studentId was not found!'});
      } else {
        rows.forEach(row =>
          loans.push({id: `${row.id}`, studentID: `${row.studentID}`}));
        res.status(200)
          .setHeader('content-type', 'application/json')
          .send(loans);
      }
    });
  }else{
    return res.status(400)
      .setHeader('content-type', 'application/json')
      .send({error: 'Please provide required query string'});
  }
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!