Put Request NodeJS

Put Request

Put Request, in this article i will step through the sample code on how to use a put request

This Article is an extension from this 2 article (1) (2)
Read the First and Second Article before you proceed to this article.

 

The Objective of this Program is to replace the existing content inside the “ingredients ” Array by using the “Put ” Request.

  • First , we need to use the app.put(‘/ingredients/:ingredientId’,function(request,response) Put Request Function
  • the Above function means that when the User query the ” http://localhost:3000 /ingredients/(“The Ingredient Id”) , the program will iterate through and find whether the Ingredient Id exist
  • If yes the the Program will update the Id with the new User Input base on the ID Provided by the User
  • ” if(!text ||text ===””) ” ,so if there is no text, tell the User , he or she need to input a text input.
  • else if the User input a text input “for(var x=0 ; x < ingredients.length;x++)" iterate through the ingredients array and find whether the Id exist or not
  • Let say during the Iteration , the program found the Object, update the New Value input by the User and break out of the For loop
  • The Program have found the Object Turn on the ” objectFound = true;”
  • After Breaking out of the Loop tell if object not found , let the User knows the Error
  • If found the Object Print out the “ingredients ” array list
  • 
    var express =require('express');
    var app =express();
    
    var bodyParser = require('body-parser');
    
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({extended: false}));
    
    var ingredients=[
        
        {
            
            "id":"12345",
            "text" : "user"
            
        },
        
        
        {
            
            "id":"45678",
            "text" : "user2"
            
        },
        
        
        
        {
            
            "id":"8888",
            "text" : "user3"
            
        }
        
        
        
        
    ];
    
    app.get('/ingredients',function(request,response){
        
        response.send(ingredients);
    });
    
    
    app.post('/ingredients',function(request,response){
        
        var ingredient = request.body;
        if(!ingredient || ingredient.text === ""){
            
            response.status(500).send({error:"Your Ingredient must have a String"});
            
            
        }else{
            
            ingredients.push(ingredient);
            response.status(200).send(ingredients);
            
        }
        
        
        
        
    });
    
    
    
    app.put('/ingredients/:ingredientId',function(request,response){
        
        var ingredientId =request.params.ingredientId;
        var text = request.body.text;
        
        if(!text ||text ===""){
            
            response.status(500).send({error:"You need to provide Ingredient Text"});
            
        }else{
            
             var objectFound = false;
             for(var x=0 ; x < ingredients.length;x++){
          
                   var ing = ingredients[x];
          
                    if(ing.id ===request.params.ingredientId){
                        
                        ingredients[x].text = text;
                        objectFound = true;
                        break;
                        
                    }
              }
            
            
            if(!objectFound){
                
                response.status(500).send({error:"Ingredient id not found"});
                
                
            }else{
            
            
            
            respond.send(ingredients);
            }
            
        }
    
     
    
    });
            
            
            
    app.listen(3000,function(){
        
        console.log("My First API Running on port 3000");
    });
    
    
    

    Before Updating

    After Updating

    After Updating the “User ” text change to “Brian888”

    Leave a Reply

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

    fifteen + 20 =