Posts

Showing posts with the label Node JS

Standard Way To Setup Node JS Project With TypeScript

Image
As you know in programming world  JavaScript  is becoming quite famous programming language so in this blog we will discuss how we can create fresh project of  NodeJS with TypeScript  in standard way. What is Node JS? It s a cross-platform, open-source, and server-side JavaScript runtime environment that executes JavaScript code outside a web browser. What is TypeScript? TypeScript is an open-source language maintained and developed by Microsoft. It's loved and used by a lot of software developers around the world. If you want to Setup with only Node JS Then here is the Link . Requirements Any server should be installed either Apache or Nginx. NodeJS and NPM should be  installed  in the operating system.  First open your terminal and go to your project directory then run following commands ~ npm init (after hitting this command it will ask some basic questions you can answer it as per your needs) { " name " : " kafka-nodejs-real-time-app " , ...

Rate Limiting in Node with Express

Image
Rate Limiting is Most Common Practice to Limit The Incoming Request of API's  Rate Limit in Node.js - Education Funda Let's see how we can achieve it easily in Node with Express framework: You need to install rate limit package via ~$ npm i express-rate-limit . import * as RateLimit from 'express-rate-limit' ; const env = process . env . NODE_ENV || 'dev' ; const rateLimitRequest = Number ( process . env . RATE_LIMIT_TIME ) || 15 ; const rateLimitTime = Number ( process . env . RATE_LIMIT_REQUEST ) || 100 ; export default () => { if ( env === 'production' ) { return new RateLimit ({ windowMs: rateLimitTime * 60 * 1000 , // 15 minutes max: rateLimitRequest , // limit each IP to 30 requests per windowMs delayMs: 0 , handler: 'Rate limt exceeded, please try again later some time.' , }); } return new RateLimit ({ windowMs: 5 * 60 * 1000 , // 5 minutes max: 3000 , // ...

How To Create S3 Bucket In AWS And Connect With Node JS

Image
a AWS S3 Bucket - Education Funda Create Policy Add ARN Create User as Allow and Create Role ? Add s3-user-policy as permission Now go into user and create access and secret key

MongoDB Mongoose Use Lookup Guide

Image
In This Blog We Will See Complete Guide For Using Joins With Lookup in Mongoose MongoDB Mongoose - Education Funda Simply in MongoDB Mongoose we can use Aggregate Query with $ lookup to get data basis upon JOINS. such as below: await MainModel . aggregate ([ { $match : { "COLUMN_NAME" : value } }, { $lookup : { from : 'relation_collection_name' , localField : 'relation_collection_primary_key' , foreignField : 'this_table_foreign_key' , as : 'ALIASING' } } ]); With above query new column added with ALIASING in which relation collection data will also come. To get data in Object format instead of Array we can use $unwind like below: await MainModel . aggregate ([ { $match : { "COLUMN_NAME" : value } }, { $lookup : { from : 'relation_collection_name' , ...

Create and Deploy AWS SAM Application

Image
How To Create and Deploy Your First Lambda Function on AWS Serverless Application Modal AWS SAM - Education Funda Create Your SAM Application Software Requirements: AWS CLI ( Reference Link ) SAM CLI ( Reference Link ) Docker ( Reference Link ) AWS Serverless Architecture - Education Funda Now All Will Happen With Command Line Interface Now open Terminal and Go to your directory in which you want to create your project. $ sam init  #hit enter this command ~ This will appear 2 option, 1 AWS Quick Start and 2 Custom Template For making things simple you can 1 at moment. ~ Now you need to select template, at moment you can select Hello World Example. ~ Now it will ask for programming language Python by default and you can select other as well such as Node.js, Java, etc. ~ Now you can choose ZIP option. ~ Now it will ask starter template, you can select anyone from that Hello World Example. ~ X-Ray tracing you can enable so select Y. ~ Enable Cloud Watch now with select Y. ~ Now it ...

Deploy Angular Build With Express JS Project

Image
Deploy Angular Build in Node JS (Express JS) Server Mean Stack - Education Funda Firstly Create Angular Production Ready Build: ~ ng build (run in command line when directory is projectFolder). ~ Compress using Brotli compression the resources using the following command for i in dist/*/*; do brotli $i; done Deployment Check Angular Build: You can get a preview of your application using the ng serve --prod command that starts a local HTTP server such that the application with production files is accessible using http://localhost:4200. This is not safe to use for production usage. For a production usage, you have to deploy all the files from the dist folder in the HTTP server of your choice. Node JS/Express JS Server File Changes: //enabling CORS app . use ( cors ()); app . use ( express . static ( path . resolve ( './public' ))); //api v1 base route set app . use ( '/api/v1' , router ); //angular front site app . use ( express . static ( path . resolve ( './public/a...

Merge Multiple PDF Buffers in Node JS

Image
Let's See How We Can Merge PDF Buffers in Node JS with "pdf-merger-js" PDF Merger JS - Education Funda For merging pdf buffers we should firstly have the buffers of each pdf and we can run loop than can add each buffer for create one single merge buffer. If You Have HTML Files Which You Want to Convert to PDF Buffer and Create 1 Single Merge PDF const getNoticesPreview = async (req, templateId ) => { const caseIdsArr = req. body . caseIds . split ( "," ); var merger = new mergePdfs (); for ( let i = 0 ; i < caseIdsArr. length ; i ++ ) { let caseId = caseIdsArr[i]; let caseDetails = await caseService. getCase (caseId); let templateDetails = await templateService. getTemplate ( templateId ); let options = { format : 'A4' }; let compiled = ejs. compile (fs. readFileSync ( __dirname + 'public' + templateDetails. templateFile , 'utf8' )...

Deploy NodeJS Project in Ubuntu Server

Image
Let's See The Standard Way To Deploy NodeJS  Project with Nginx  Firstly I assume you already connected to your Ubuntu server. Now first command which you need to run is ~ sudo apt-get update (This command will update all default packages of ubuntu server with latest versions) Now let's install Nginx server ~ sudo apt install nginx Now you have to install Node JS and NPM (Please read this article for it) Now you can check with ~ nginx -v ~ node -v ~ npm -v  NodeJS - Education Funda In each above commands version should be written if not it means it not installed properly in your server. now go to cd /var/www directory -> From here create directory sudo mkdir domain.xyz . -> cd domain.xyz . From here take clone from your GIT branch ( Reference Link ) Now go to ~ cd yourCloneDirectory . -> From this directory just run your project ~  node app.js/server.js (your root file of node js project) Now just call your IP or Domain in your browser your project should...

Installing Node.js in Ubuntu Server PPA

Image
Installing Node.js in Ubuntu Server with Apt Using a NodeSource PPA Node JS Installation - Education Funda To install a different version of Node.js, you can use a PPA (personal package archive) maintained by NodeSource. These PPAs have more versions of Node.js available than the official Ubuntu repositories. Node.js v14, v16, and v18 are available as of the time of writing. First, we will install the PPA in order to get access to its packages. From your home directory, use curl to retrieve the installation script for your preferred version, making sure to replace 18.x with your preferred version string (if different). curl -sL https://deb.nodesource.com/setup_18.x -o nodesource_setup.sh --------------------- You can inspect the contents of the downloaded script with nano (or your preferred text editor): nano nodesource_setup.sh ---------------------------- Running third party shell scripts is not always considered a best practice, but in this case, NodeSource implements their own logi...

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...