Skip to main content
Fix formatting and tags
Source Link
eush77
  • 4.1k
  • 1
  • 26
  • 31

Query parameters in Express Route Parameter

I am trying to get access to query parameters using expressExpress in nodeNode.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).

Express Route Parameter

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).

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).

I am trying to get access to routequery parameters using express in node.js. For some reason, "req.body"req.params keeps coming up as an empty object. Here is my code in server.jsserver.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"http://localhost:3000/scanned?orderid=234. The console log that I currently have in the routes.jsroutes.js file is showing an empty body (not recognizing "orderid"orderid parameter in the URL).

I am trying to get access to route parameters using express in node.js. For some reason, "req.body" 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).

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).

deleted 132 characters in body
Source Link
Jim M
  • 351
  • 2
  • 6
  • 13

I am trying to get access to route parameters using express in node.js. For some reason, "req.body" 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');
});


//url posted to for rfid scanff
routes.post('/rfidscan'scan',  (req, res) => {
    res.status(200);
    // const ORDERID = req.body.orderID;
    res.send("hello");
});


//used for view of order details after an order update
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).

I am trying to get access to route parameters using express in node.js. For some reason, "req.body" 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');
});


//url posted to for rfid scanff
routes.post('/rfidscan',  (req, res) => {
    res.status(200);
    // const ORDERID = req.body.orderID;
    res.send("hello");
});


//used for view of order details after an order update
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).

I am trying to get access to route parameters using express in node.js. For some reason, "req.body" 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).

Source Link
Jim M
  • 351
  • 2
  • 6
  • 13
Loading