Posts

Showing posts from October, 2022

Loading GIF Loader In Center of Webpage

Image
While page is loading sometimes it takes too much time for that time duration we can show GIF loader or any kind of loader to the users so users can know easily something is happens. Add Loader To Your Webpage   Such as your webpage html code is like below: <body>     <div class="content">         Long Content Here         <br/>         Click to view loading image.     </div>     <div id="divLoading" style="display:none">     </div> </body> We added one div here with the ID divLoading, you can take any ID selector as per your need. Now in webpage you need to add css code so that loader can be show in center of page at any screen size: <style type="text/css">      #divLoading.show      {          display : block;          position : fixed;          z-index: 100;          background-image : url('spinner.gif');          background-color:#666;          opacity : 0.4;          background-repeat

Node JS Convert 404 API Route to Proper Message

Image
We Generally Face A Problem of Bad Error Handling If Route Not Found in Node JS, Let's See How Can We Fix This Error Handling in Node JS Via adding below 4 lines code in our main node js file, we can manage it easily: //404 error handling app . use (( req , res ) => { const error = new Error ( "Not found." ) req . code = 1 req . statusCode = 404 ResponseMiddleware ( req , res , error . message ) }) That's it ! Happy Coding !

Manage Validations with Node Input Validator in Node JS

Image
Standard Way to Manage Validations in Node JS with Node Input Validator Package Node JS - Education Funda As per standard code in node js we should prefer manage validations in separate directory and files it means we can create separate directory such as validations and inside it we can create index.js file which is common file and manage separate validation for each module such as userValidatoin.js, postValidation.js etc. Something like this we can manage directory structure. Let's go deep into code now:- validations/index.js file: const validator = require ( "node-input-validator" ) const ResponseMiddleware = require ( "../middlewares/ResponseMiddleware" ) const fn = require ( '../helpers/functions' ) module . exports = { //common function to send validation response validate : ( v , res , next , req = null ) => { console . log ( "ValidatorsIndex => validate" ) if ( v . check (). then ( function ( ma

NodeJS Standard Way to Write Models with Sequelize

Image
As we all know in our node js project there is one main file which is generally index.js or app.js . so if we put all code related to models file load so that is not an standard way of coding so for solving this problem we can create model directory , inside this directory we can create one index.js file in which we load all the models then call in models/index.js file in our main file.  Node JS Here is our app.js file: "use strict" require ( 'dotenv' ). config (); //for calling process.env.VAR_NAME globally const server = require ( './config/server' ), express = require ( 'express' ), app = express (), port = server . port ; require ( './src/models' ); // all routing app . get ( '/' , ( req , res ) => { res . send ( server . appName ) }) app . use ( '/api' , require ( './src/routes/index' )); //listen server on mentioned port app . listen ( port , "0.0.0.0" , () => { console . log ( `

Authenticate Node JS Rest APIs With JWT

Image
Let's See How We Can Authenticate Node JS Rest APIs With JWT Package Firstly install the jwt package in your node js project via below command: $ npm install jsonwebtoken Create User Token For creating token we can manage it in middleware . Let's say create AuthMiddleware.js file: const jwt = require ( 'jsonwebtoken' ); const createUserToken = async ( id , email ) => { try { const token = jwt . sign ({ "user_id" : id , "email" : email }, process . env . JWT_TOKEN_KEY , { expiresIn : process . env . JWT_TOKEN_EXP //2h, 30d }) return token } catch ( err ){ return '' } } module . exports = { createUserToken } Now in controller we can call this function for create token. Let's say we have UserControoler.js file inside which we are creating user token: const AuthMiddleware = require ( '../middlewares/AuthMiddleware' ); const create = async ( req , res , ne