Route Middleware to Check if a User has active subscription in Node.js

Route Middleware to Check if a User has active subscription in Node.js

In this post we will give you information about Route Middleware to Check if a User has active subscription in Node.js. Hear we will give you detail about Route Middleware to Check if a User has active subscription in Node.jsAnd how to use it also give you demo for it if it is necessary.

In this Node.js tutorial, I will tell you how to implement route middleware to check if a user has active subscription or not in Node.js APIs.

With subscription based application, There must be some restriction to give full access to user after expiration of plan.

So you can easily define a middleware which acts as a middle part between request and response for those routes which will be accessible after subscription.

You can also implement route middleware to check user authentication.

Middleware are just like a function with request object, response object and next method in the application request and response cycle.

For this example, I have a table called “users” having column “subs_ends_at” and I need to check if a user has active subscription then user can access the api otherwise user will get response with “HTTP Status Code 401” and column “subs_ends_at” will manage the end date of subscription.

Route middleware to check user subscription

  1. functionvalidateSubscription(req, res, next)
  2. {
  3. var userId=(req.method =="GET")? req.query.userId : req.body.userId;
  4. client.query("select subs_ends_at from users where id=?",[userId],function(error, result, fields){
  5. if(error)
  6. {
  7. res.status(500).send({"error":"true","message":"Something went wrong","result":{}});
  8. }
  9. elseif(result.length ==)
  10. {
  11. res.status(200).send({"error":"true","message":"Sorry, User does not exists.","result":{}});
  12. }
  13. else{
  14. if(moment(result[].subs_ends_at).diff(newDate(),'days')>){
  15. next();
  16. }else{
  17. res.status(402).send({"error":"true","message":"Sorry, Your subscription has been expired.","result":{}});
  18. }
  19. }
  20. });
  21. }
function validateSubscription(req, res, next) 
  var userId=(req.method == "GET") ? req.query.userId : req.body.userId;        
  client.query("select subs_ends_at from users where id=?", [userId], function (error, result, fields) {
     res.status(500).send({ "error": "true", "message": "Something went wrong","result": {}});
   else if(result.length ==0)
     res.status(200).send({ "error": "true", "message": "Sorry, User does not exists.","result": {}});        
    if(moment(result[0].subs_ends_at).diff(new Date(),'days') > 0){
      res.status(402).send({ "error": "true", "message": "Sorry, Your subscription has been expired.","result": {}});        


Above middleware will be implemented after login routes to check subscription status of logged in user by their user id.

First, i get the user id from request that can have “GET” or “POST” HTTP method.

There can be get and post both routes so i use req.method to get HTTP method in the middleware.

moment.diff will return the difference in days between two dates. For more details click here to calculate datetime difference

next() is not a predefined method of the Node.js, It is just a third argument that is passed to middleware method. If you want then you can rename it with anything but it is very necessary to avoid confusion by using valid naming convention.

Implementing “validateSubscription” middleware with protected routes

Now i implement the “validateSubscription” middleware with routes to restrict user to access protected routes.

  1. module.exports =function(app){
  2. app.get('/dashboard', validateSubscription,function(req, res){
  3. res.send('Welcome to protected dashboard!');
  4. });
  5. };
module.exports = function(app) {
  app.get('/dashboard', validateSubscription, function(req, res) {
      res.send('Welcome to protected dashboard!');

Now if a user has active subscription then he will be able to access the dashboard routes.

Try this..

Label :


How To

Web Development




Hope this code and post will helped you for implement Route Middleware to Check if a User has active subscription in Node.js. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

7  +  3  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US