Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

129
Visualizações
Express JS url parameter variable issue

I have this user route

const express = require('express');
const router = express.Router();

const {
    GetAll,
} = require('../areas/directory/controllers/usercontroller');


router.route('/getall?:username&:email').get(GetAll);


module.exports = router;

When I try to access the url in Postman like this: http://localhost:5000/api/user/getall?username=nameone&email=emailone

I get this error: Cannot GET /api/user/getall

But, if I change it to

router.route('/getall/:username&:email').get(GetAll);

and access the url in Postman like this: http://localhost:5000/api/user/getall/username=nameone&email=emailone

it works.

On the other hand, even if it works, I am unable to get the value of my variable.

var username = req.params.username;

will return username=nameone instead.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

For http://localhost:5000/api/user/getall?username=nameone&email=emailone to work, you should change

router.route('/getall?:username&:email').get(GetAll);

to

"router.route('/getall').get(GetAll);"

and use req.query.username to access the value of username query parameter and req.query.email to access the value of email query parameter.

After making these changes, you can call http://localhost:5000/api/user/getall?username=nameone&email=emailone in Postman, you will be able to see the value of username and email in your code.

This is because you need not have to specify the query parameters in the path, like ?:username in router.route('getall').

Edit: adding few more details about path and query

Please see the top 2 solutions for this question to learn more about path and query and why you should change your code to the way I mentioned above : here is the link.

about 4 years ago · Juan Pablo Isaza Relatório

0

Reason for the error...

Actually the thing what you are trying to do is called query passing in the rest api..

But you are trying to access them like params

sollution 💖

follow the steps to access the queries

**To pass the data using ? in the api request should be like this you can have multiple query objects

http://localhost:5000/getall?email='email'&&password='password'

Access the query in express app like this

app.post('/getall', (req, res) => {
const {
    email,
    password
} = req.query
return res.status(200).json({
    email,
    password
})

})

The req.query contains the data you trying to pass throw ? and then query properties

Response of above code would be like this

{
"email": "'email'",
"password": "'password'"
 }
about 4 years ago · Juan Pablo Isaza Relatório

0

You should not give query parameters in URL. So, for express the url must be just /getall and in your code you access those variables using req.params.whatever_query_parameter

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda