Search code examples
node.jsexpressjwtjson-web-token

How to get JWT token from URL using Express server?


Supposing the URL is like http://localhost:3000/auth?token=helloworld

How to get the jwt token from this url using express server? I have tried this code below.. no output.

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

    router.use(function(req, res, next) {
    var token = req.query.token;
    console.log(token);
    try {
        var decoded = jwt.verify(token, 'thisismysecretstring');
        console.log(decoded);
        res.send(req.query.token);
    } catch (err) {
        console.log(err);
    }
});

app.use('/auth', router);

Solution

  • The URL format should be like this:

    http://localhost:3000/auth/?token=helloworld
    

    Now, it is working. Thanks.