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

184
Views
node GET request won't show up on html page, only loads - doesnt return error

When I send a route.get request from the serve it responds with JSON data successfully and displays on localhost:/view/product page, however, I want it to process my client side html page and display the database info into the table there. It is not doing that.

My goal is to GET this data and display it into a simple html table on the localhost:3000/view/product route.

Any advice would be appreciated, thank you in advance.

My html table attempt:

<html> 

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="app.css">
  <link rel="stylesheet"
  href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.no-icons.min.css">
  <div>
     <header> 
      <h2><a href="/"><i></i> VIEW PRODUCTS  </a></h2>
     </header>
  </div>
</head>

<body>
<div id="table">
  <table align='center' cellspacing=2 cellpadding=5 id="productTable" border=1>
      <thead>
          <th> Product Name </th>
          <th> Quantity </th> 
          <th> Price </th>
      </thead>
      <tbody id="productTable"></tbody>
  </table>
</div>
</body>
</html>

<script>
$.get('/view/product', {}, function(results) {
  let res = '';
  results.forEach(data => {
    res += `<tr><td>${data.productname}</td><td>${data.quantity}</td><td>${data.price}</td></tr>`
  })
  $('#place-here').html(res)
});

</script>

my get request:

 route.get("/view/product", async function (req, res) {
      try {
        const results = await db.query("SELECT * FROM product ORDER BY createdAt DESC;",
       );

        // console.log(results);
        // res.status(200).json({
        //   status: "success",
        //   results: results.rows.length,
        //   data: {
        //     orders: results.rows,
        //   },
        // });
      } catch (err) {
          console.log(err);
      }
    });    

loading the html page in my index.js file:

app.get('/view/product',function(req,res){
  res.sendFile('view.html', { root: __dirname });
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can't use the same path for both your page load and for the Ajax call to fetch JSON as they are both GET routes so only the first one registered with your Express server will ever get a chance to see the incoming request.

I'd suggest you change the Ajax call to something like /api/products and then change the corresponding definition on your server for the route that returns JSON.

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!