mocha/supertest/nodejs How To Access Request Parameters in URL?

3,601 views
Skip to first unread message

JPJen

unread,
May 5, 2014, 3:19:30 PM5/5/14
to moc...@googlegroups.com
Hi, it is me again.

Many thanks to Miroslav for helping me to get my first mocha/supertest working.

Now, there is a query string parameter in the URL (please refer to what marked in red in the code snippet below).  How do I retrieve the value of that query parameter "limit"?  I have tried the way to access query parameter in NodeJS; i.e.,

var limit = request.query.limit;

I have also tried:

var limit = request.params.limit;

both give TypeError: Cannot read property 'limit' of undefined.


The code below works except that there is a query parameter added.

    var supertest = require('supertest');
    var request = supertest('localhost:3001');

    describe('GET record_limit_warning', function() {


        this.timeout(15000);
        it ('Check header message', function(done) {
            request.get('/ecrud/v1/core/dbq/534e930204dd311822ec1c9d?limit=20')
                .expect('warning', '100 Max Record Limit Exceeded')
                .expect(200, done);
        } );
    } );

Miroslav Bajtoš

unread,
May 6, 2014, 8:01:05 AM5/6/14
to moc...@googlegroups.com
On Monday, May 5, 2014 9:19:30 PM UTC+2, JPJen wrote:
Now, there is a query string parameter in the URL (please refer to what marked in red in the code snippet below).  How do I retrieve the value of that query parameter "limit"?  I have tried the way to access query parameter in NodeJS; i.e.,

var limit = request.query.limit;

I have also tried:

var limit = request.params.limit;

both give TypeError: Cannot read property 'limit' of undefined.

The answer depends on the implementation of your server that is listening on localhost:3001. Please post the relevant code.

BTW, this question is rather off-topic for this group. General NodeJS related question should be asked in the nodejs group:


Miroslav

JPJen

unread,
May 6, 2014, 10:21:03 AM5/6/14
to moc...@googlegroups.com
Thanks to Miroslav for your continuing support.

I am sorry that I did not post the code snippet.  Anyway, I have tried to search on the internet for a solution and not yet gotten my code to work.  Below is my latest code.  The error message is "Cannot find module '/ecrud/v1/core/dbq/534e930204dd311822ec1c9d?limit=1' ".  Somehow, I think I need the 'url' module and let the NodeJS know that '/ecrud/v1/core/dbq/534e930204dd311822ec1c9d?limit=1' is a URL.

    var supertest = require('supertest');
    var request = supertest('localhost:3001');
    var url= '/ecrud/v1/core/dbq/534e930204dd311822ec1c9d?limit=1';

    describe('GET record_limit_warning', function() {
        this.timeout(15000);
        it ('Check header message', function(done) {

            var query = require(url).parse(request.url,true).query;  // This line has the error
            var limit = query.limit;
            console.log(limit);

            request.get(url)

              .expect('warning', '100 Max Record Limit Exceeded')
              .expect(200);
            done();
        } );
    } );

Thanks for your attention and help.  I recently switch from Java coding to NodeJS coding.

JPJen

unread,
May 6, 2014, 11:40:06 AM5/6/14
to moc...@googlegroups.com
Hi, I have managed to make the code work.  Thank you very much.  I post the code below:


var supertest = require('supertest');

var request = supertest('localhost:3001');

var url = require('url');

 

describe('GET record_limit_warning', function() {

    this.timeout(15000);

    it ('Check header message', function(done) {

        var queryParams = url.parse("/ecrud/v1/core/dbq/534e930204dd311822ec1c9d?limit=1", true);

        var limit = queryParams.query.limit;

        console.log(limit);

        request.get(url)

            .expect('warning', '100 Max Record Limit Exceeded')

             .expect(200);

          done();

    });

});

Reply all
Reply to author
Forward
0 new messages