6

I am using node.js restify.

I have a HTTP GET request that looks like this;

http://127.0.0.1//read_val?XXX=123&YYY=456&ZZZ=789

In my handling function, to retrieve the URL parameters, the relevant code will be like this;

var api_get_func = function (app, url_path) {
    function respond(req, res, next) {
        var XXX= req.query.XXX;
        var YYY = req.query.YYY;
        var ZZZ = req.query.ZZZ;

        //SQL query ...
        return next();
    }

    app.get(url_path, respond);
} 

Now, what if I have a HTTP GET function like this below

http://127.0.0.1//read_val?XXX=123&YYY=456

The ZZZ parameter is not provided in the URL. How do I modify the code such that ZZZ will use a default value of, say, 111?

3 Answers 3

8

If just want to check if something is provided, then you could just do:

var ZZZ = req.query.ZZZ || 111;

But... GET parameters are query strings, so we probably want to make sure it is a number.

if (!parseInt(req.query.ZZZ)) {
  req.query.ZZZ = 111;
}

Or if you want to get ternary with it:

req.query.ZZZ = parseInt(req.query.ZZZ) ? req.query.ZZZ : 111;

Do note that the other parameters are a string and that this default is being set as a number. So, you might want '111' as opposed to 111. Also, you can parseInt all of your query strings or toString them all if they are all a number, just try to make sure they all remain the same expected type. Unless of course these are all strings of text, in which case ignore all this.

Sign up to request clarification or add additional context in comments.

1 Comment

'' is not truthy; the rest of this follows though
2
var api_get_func = function (app, url_path) {
    function respond(req, res, next) {
        var XXX= req.query.XXX;
        var YYY = req.query.YYY;
        var ZZZ = req.query.ZZZ || <YOUR DEFAULT VALUE>;

        //SQL query ...
        return next();
    }`enter code here`

    app.get(url_path, respond);
} 

5 Comments

looks awesomely simple. Is this solution applicable only to node.js restify?
It is a general JavaScript thing. It is similar to doing if (req.query.ZZZ || 111) as the truthy value is returned.
you can use this as well if this value can be 0 then you can use simple tertiary operator(?:) as like
req.query.ZZZ === ' undefined' ? 'default ' : req.query.ZZZ
@Atul Agrawal, your answer is good but I think Kevin Reilly's is better. Sorry, I can only mark one answer as the answer.
1

In one line, the best way is this:

let limit = parseInt(req.query.limit || 0);
let page = parseInt(req.query.pagina || 1);
let offset = page * limit

Because you put a default value then you parse the content.

req.query.limit || 0

This line check if the content was sent, if not, put 0 as default value

1 Comment

Beware that parseInt might return NaN values if the req.query.limit is "abc" as example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.