Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

245
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda