Posts

Nodejs-Assignment-8

 Create following routes in User API  1. Get user by username (Password should not be included)  2. Get all users (Passwords should not be included)  3. Login user Website links: https://jwt.io/ https://www.npmjs.com/ Sample code for User login: ---------------------------------- userApp . post ( ' /user-login ' , expressAsyncHandler ( async ( request , response ) => { //get userCollectionObj const userCollectionObj = request . app . get ( " userCollectionObj " ); //get user credentials from req const userCredObj = request . body ; //verify username let userOfDB =await userCollectionObj . findOne ({ username : userCredObj .username}) //if username is invalid if ( userOfDB === null ){ response . status ( 200 ). send ({ message : " Invalid username " }) } //if username is valid else { //verify password let isEqual =await bcryptjs. compare ( userCredObj .password, userOfDB .password) //if passwords not...

Nodejs-Assignment-7

 Create User API & Product API using async & await . Handle asynchronous errors using try/catch OR express-async-handler module. User API for ref --------------------- //create mini-express app(A router) const exp = require ( " express " ); const userApp = exp . Router (); const expressAsyncHandler = require ( " express-async-handler " ); const bryptjs = require ( " bcryptjs " ); //body parser middleware userApp . use ( exp . json ()); //CREATE user API //create user userApp . post ( " /user-signup " , expressAsyncHandler ( async ( request , response ) => { //get userCollectionObj const userCollectionObj = request . app . get ( " userCollectionObj " ); //get newUser from request const newUser = request . body ; //check for duplicate user by username let userOfDB = await userCollectionObj . findOne ({ username : newUser .username, }); //if user already exist...

Nodejs-Assignment-6

 Implement products api to perform CRUD operations with DB     1. Create product     2. Get all products     3. Get product by name     4. Get product by ID     5. Update product details     

MongoDB operations-2

  -> Change age of “madhu” to 35 db.users.updateOne({name:"madhu"},{$set:{age:35}}) -> Increate salary of “madhu” by 10000 db.users.updateOne({name:"madhu"},{$inc:{salary:10000}}) -> Remove "age" field for "Madhu" db.users.updateOne({name:"madhu"},{$unset:{age:""}}) -> Remove "city" property of"address" field for "Madhu" db.users.updateOne({name:"madhu"},{$unset:{"address.city":""}}) -> Add "street" property of"address" field for "Madhu" db.users.updateOne({name:"madhu"},{$set:{"address.street":"KPHB"}}) -> Add new skill “java” to user “Madhu” db.users.updateOne({name:"madhu"},{$addToSet:{skills:"java"}} ) -> Add multiple skills to ”madhu” db.users.updateOne({name:"madhu"},{$addToSet:{skills:{$each:["abc","xyz"]}}})  //no dup...

MongoDB operations-1

 Follow the below link to install and configure MongoDB community server & mongo shell https://drive.google.com/file/d/1lrBr3ftngnkdXc2Oidm31iVUnEUJJ_ci/view?usp=sharing Practice the following DB CRUD operations. 1. Create user collection and insert 5 user documents in it. { name : “ xxxxxx” , age : xx , salary : xxxxxx, skills : [ xxxx , xxxx , xxxx ] , address : {   city:”xxx”, pincode:xxxxxx      } } 2.    Find all users db.users.find() 3. Find user with name “Ravi” db.users.find({name:”ravi”}) 4. Find users from city Hyderabad db.users.find({"address.city":"hyderabad"}) 6. Find users whose salary is greater than 30000 db.users.find({salary:{$gt:30000}}) 5 . Find users whose age between 20 and 25 db.users.find({$and :[{age:{$gt:20}},{age:{$lt:25}}]}) 6. Find names of all users db.users.find({},{name:1,_id:0}) 7. Find the users    with ”angular” as    a skill db.users.fin...

Nodejs-Assignment-5

   Create two APIs User API & Product API  Each API should implement following services              Create resource               Read all resources                Read a resource by id or name               Update resource               Delete resource by id Access user routes using    http://localhost:4000/user-api/ Access user routes using    http://localhost:4000/product-api/ Implement  middlewares  to  handle invalid path of client request and synchronous errors  in routes.

Nodejs-Assignment-4

 Create 3 middleware functions.      Execute first middleware for every client request.      Execute second  & third middlewares for post request.     Remove next() in second middleware and check the behaviour.     Write a middleware to deal with invalid paths     Write another middleware to deal with errors