Posts

Showing posts from October, 2021

Multer file upload and delete - Nodejs, Mongodb, Express

Multer is a node.js middleware  which is primarily used for uploading files. Dependencies     "express" : "^4.17.1" ,     "mongoose" : "^6.0.9" ,     "multer" : "^1.4.3 Multer doc :  https://www.npmjs.com/package/multer Middleware required : Path and Multer Folder to upload file and name of a file. const storage = multer . diskStorage ({     destination : function ( req , file , cb ) {         cb ( null , path . join ( __dirname , '../uploads' )) //upload file in uploads folder       },       filename : function ( req , file , cb ){         const uniquePrefix = Date . now () + '-' + Math . round ( Math . random () * 1E9 )         cb ( null , uniquePrefix + '-' + file . originalname )   //uploaded file name     } }) Filter file types  const fileFilter = ( req , file , cb ) => {     // The function sho...

Nodemailer setup on NodeJS Express MongoDB

"dependencies" : {     "dotenv" : "^10.0.0" ,     "express" : "^4.17.1" ,     "mongoose" : "^6.0.11" ,     "nodemailer" : "^6.7.0" SMTP Transport config/mail.js mailtrap.io is free email checker for developers. const nodemailer = require ( 'nodemailer' ) require ( 'dotenv' ). config () module . exports = nodemailer . createTransport ({     host : process . env . CURRENT_ENVIRONMENT == "development" ? "smtp.mailtrap.io" : "" ,     port : 587 ,     secure : false , // upgrade later with STARTTLS     auth : {       user : process . env . SMTP_USERNAME ,   //from .env       pass : process . env . SMTP_PASSWORD     },   }) Message configuration utils/sendmail.js const transporter = require ( '../config/mail' ) module . exports = (({ from , to , subject , text , html }) => {       var message = {         from ...

Maximise top

Description You are given a stack of N integers. In one operation, you can either pop an element from the stack or push any popped element into the stack. You need to maximize the top element of the stack after performing exactly K operations. If the stack becomes empty after performing K operations and there is no other way for the stack to be non-empty, print -1. Input Input Fomat The first line of input consists of two space-separated integers N and K. The second line of input consists N space-separated integers denoting the elements of the stack. The first element represents the top of the stack and the last element represents the bottom of the stack. Constraints N < 2000000 K < 10^9 Numbers of stack <10^18 (Note that numbers are very large) Output Output Format Print the maximum possible top element of the stack after performing exactly K operations. Sample Input 1  6 4 1 2 4 3 3 5 Sample Output 1 4