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

243
Views
how to deal with url in node.js?

I'm a student and I'm doing my H.W.

My goal is, when I click an item, send its id through URL and deal with in node.js

For example,

http://localhost:3000/product/:2

is the url when I clicked the item which has '2' as its ID.

But I don't know how to deal with it in node.js.

app.get('/searched.html',async function(req,res){
    let db = await getDBConnection();
    let rows = await db.all('select * from items');
    await db.close();

    var output=new Array();
    var id =  new Array();
    var query = url.parse(req.url,true).query;

    if(query.productSearch=='All'||query.productSearch==''){
        for(let i=0;i<rows.length;i++){
            output.push(rows[i]['product_image']);
            id.push(rows[i]['product_id']);
        }   
    }else{
        for(let i=0;i<rows.length;i++){        
            if(rows[i]['product_category']==query.productSearch){
                output.push(rows[i]['product_image']);
                id.push(rows[i]['product_id']);
            }
        }
    }
    
    var output_send = JSON.stringify(output);
    var id_send = JSON.stringify(id);

    res.render('index',{
        data: output_send,
        data2 : id_send
    });
});

The code above is the code what I used to deal with query String.

I guess I can reuse it, but I don't know how to fix it appropriately.

Could you give me some hints?

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

0

You can get parameter from url like this :

/product/2

app.get('/product/:id', async function(req, res) {
  const id = req.params.id; // output : 2
});
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!