-1

I'm using express in a nodejs project to call an endPoint and print the parameters after it in console. The url can be:

/printInfo?id=xxx&value=xxx 

or

/printInfo?id=xxx

or

/printInfo?value=xxx

How can I do this?

Araf
  • 263
  • 1
  • 5
  • 19

2 Answers2

1

Assuming you just want to understand how to read the query string, you just read the values on the req.query variable. Here is a simple setup:

routes/index.js

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

router.get('/printInfo', (req, res, next) => {
  res.send({ id: req.query.id, value: req.query.value});
});

module.exports = router;

app.js

const express = require('express');
const indexRouter = require('routes/index');

const app = express();

app.use('/', indexRouter);

app.listen(3000, () => console.log(`Example app listening on port 3000!`));

Now, when you make a request to http://localhost:3000/printInfo?id=1&value=test you should see (I have the JSON Formatter extension installed):

{
  "id": "1",
  "value": "test"
}

Show up at that page.

Here is a gif showing how it looks on my machine:

gif of req.query

djs
  • 3,947
  • 3
  • 14
  • 28
  • I want to print those values in command prompt and pass them in another function. I need those values in my script file. I'm using express for the first time. sorry, if it's a dumb question – Araf Dec 18 '19 at 05:36
  • 1
    @Araf You can print the values with console.log() just like any other JS value. To pass them to a function, just pass the req.query.paramName to a function, just like any other value. Maybe post a specific example and I can help? – djs Dec 18 '19 at 05:54
  • thanks. I got it now. It's almost like normal app.get function. thanks a lot – Araf Dec 18 '19 at 06:09
  • Ohhh the router.get() was confusing. Sorry, I forgot that most examples don't start with that, I've been in the Express weeds for weeks and it slipped my mind. – djs Dec 18 '19 at 06:12
0

It's not entirely clear to do with the data you get from the URL, but req.query contains whatever query parameters are in the URL and you can just iterate that object to see what's there:

 for (let prop of Object.keys(req.query)) {
     console.log(prop, req.query[prop]);
 }

And, here's a simulated demonstration which can run in a local snippet, but you can use the same type of code on req.query in Express:

    // define simulated req.query (this is built automatically in Express)
    let req = {
        query: {id: 34506, value: "$400.99"}
    };

    // iterate arbitrary properties of req.query
    for (let prop of Object.keys(req.query)) {
        console.log(prop, req.query[prop]);
    }

Or, if you know what possible query parameters may be there and you just want to test which ones are there, you can just do like this:

if ("id" in req.query) {
    // id property is present
    console.log(req.query.id);
}
if ("value" in req.query) {
    // value property is present
    console.log(req.query.value);
}
jfriend00
  • 683,504
  • 96
  • 985
  • 979