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

226
Vistas
Query parameters in Express

I am trying to get access to query parameters using Express in Node.js. For some reason, req.params keeps coming up as an empty object. Here is my code in server.js:

const express    = require('express');
const exphbs     = require('express-handlebars');
const bodyParser = require('body-parser');
const https      = require('https');

//custom packages  ..
//const config  = require('./config');
const routes  = require('./routes/routes');


const port = process.env.port || 3000;


var app = express();

//templating engine Handlebars
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');





//connect public route
app.use(express.static(__dirname + '/public/'));


app.use(bodyParser.json());

//connect routes
app.use('/', routes);






app.listen(port,  () => {
    console.log( 'Server is up and running on ' + port );
});

And here is my routes file:

//updated
const routes = require('express').Router();


routes.get('/', (req, res) => {
  res.render('home');
});



routes.post('/scan',  (req, res) => {
    res.status(200);

    res.send("hello");
});



routes.get('/scanned',  (req, res) => {

    const orderID = req.params;
    console.log( req );

    res.render('home', {
        orderID
    });
});

module.exports = routes;

When the server is up and running, I am navigating to http://localhost:3000/scanned?orderid=234. The console log that I currently have in the routes.js file is showing an empty body (not recognizing orderid parameter in the URL).

about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

orderid in the request is query parameter. It needs to be accessed via req.query object not with req.params. Use below code to access orderid passed in the request:

const orderID = req.query.orderid

Now you should be able to get 234 value passed in the request url.

Or try replacing the code for route /scanned with below:

routes.get('/scanned',  (req, res) => {

  const orderID = req.query.orderid
  console.log( orderID ); // Outputs 234 for the sample request shared in question.

  res.render('home', {
    orderID
  });
});
about 4 years ago · Santiago Trujillo Denunciar

0

The reason that req.body keeps coming up as an empty object is that in get requests, such as those made by the browser on navigation, there is no body object. In a get request, the query string contains the orderid that you are trying to access. The query string is appended to the url. Your code can be rewritten as below:

routes.get('/scanned',  (req, res) => {

    const orderID = req.query.orderid
    console.log( req );

    res.render('home', {
        orderID
    });
});

A note, though, is that if you have AJAX posts firing within your client side code, your req.body will not be empty, and you can parse it as you would normally.

about 4 years ago · Santiago Trujillo 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