Secured Web API using ExpressJS and JSON Web Token in Visual Studio
Hi Guys, This time I created a secured Web API using ExpressJS and JSON Web Token in Visual Studio. Hope you guys like it. 1) Create Express JS Web Application in Visual Studio 2) Add jsonwebtoken by right clicking on npm -> Install new npm Packages 3) Create a file named api.js which will be our secured api and paste the following code 'use strict'; var express = require('express'); var router = express.Router(); var jwt = require('jsonwebtoken'); /* GET home page. */ router.get('/', function (req, res) { res.render('index', { title: 'API' }); }); //Login method to get the token router.post('/login', function (req, res) { let user = { id: 3 }; let token = jwt.sign(user, 'app_secret'); res.json({ token:token }); }); //our protected resource router.get('/protected', ensureToken, function (req, res) { jwt.verify(req.token, ...