1

Hi all I'm new to web development so I'm developing a simple online recipe book to allow me to also learn node and angular in the process.

How ever I'm struggling with what type of responses I should send back to the client for success and fails, in the case of post requests, or delete requests.

Here's an example of an api POST access point:

app.post('/recipe/',(req,res)=>{

res.set({
    'Access-Control-Allow-Origin': 'http://localhost:4201'
  })

  console.log("Recieved req at /recipe/")
  if(req.body.recipe === undefined){
      res.send("Request not filled. No recipe found");
  }
  if(req.body.ingredients === undefined){
      res.send("Request not filled. No ingredients found");
  }
  //var r = new Recipe(req.body.recipe,req.body.ingredients);
  mongo.addRecipe(req.body.recipe,req.body.ingredients).then((promise)=>{
      mongo.getAllRecipeByName().then(promise=>{
          console.log(promise);
      });

      res.send(promise);
  })
})

Here's the code for mongo.addRecipe() so you know what's being returned in that promise:

    async addRecipe(recipeName,ingredients){
   if(ingredients == null){
       return null;
   }
   if(recipeName == null){
       return null;
   }
    var recipe = new Recipe(recipeName,ingredients);
   if(recipe.getIngredients().length <= 0){
       return null;
   }

    try{
        const client = new MongoClient(this.uriString);
        
        await client.connect();
        const db = client.db(this.databaseString);
        const recipeCollection = db.collection(this.collectionString);
        
        var status = await recipeCollection.insertOne(recipe);
        client.close();
        if(status.acknowledge == true){
            return status
        }
        else{
            return status
        }
        //console.log(await recipeCollection.find({recipeName:recipeName}).toArray());
    
    }
    catch(e){
        console.error("Failed addRecipe() params: "+recipeName+" "+ingredients+e);
        return null;
    }
    //String recipeName  List<String> ingredients
    //creates connection with mongo database.
    //add {name:'recipeName',ingredients:'ingredients'} to mongoDB
    
}

So as you can see some of my verification statements return null and then I'm sending null back to the client. Is this a mistake? In the case of success I am returning the massive object that's returned from collection.insertOne();

What should I be sending to the client in the case of success and failure that fits with the industry standards?

jakegergen
  • 135
  • 1
  • 1
  • 10
  • 2
    idk about any industry standards but what you can do fix an object which you'l return on on every call. something like `{"status": true, "data": {your data}, "message":"any message here"}` . if intended action is successful then you'll return true in status or else false, any json data in `data` and corresponding message in `message` key – Rohit Ambre Aug 19 '20 at 17:23
  • 1
    @RohitAmbre Thank you for your response. This idea makes a lot of sense to me and I'm going to refactor my code to fit this model. Thanks again! – jakegergen Aug 19 '20 at 18:08
  • To be honest there is no strict rule, these are just conventions, I highly recommend take a look at https://foursquare.com as a really good example but the best practice is using http code not responses to make your server side action more clear `201` for POST action and `204` for DELETE action – MohamadrezaRahimianGolkhandani Aug 19 '20 at 20:01

0 Answers0