title: CS Node js Help: writing GET/PUT requests as specified by tests?
body: <p>I have been trying to complete this past paper but Im not sure if im on the right lines as to my solution and so wanted some help to see if I am on the right track or whether I need some guidance on getting there!</p>
  <p>the question is as follows:</p>
  <p>You have been contracted to write part of a RESTful web API for products on an e-commerce platform using Node.js. Another developer had previously been working on the project and had written the following tests using Mocha and Chai.</p>
  <pre><code>const   expect  =   require('chai').expect;
  const   request =   require('superagent');
  const   status  =   {OK:200,BADREQUEST:400};
  const   endpointURL =   
  
  'http://localhost:3000/api/v1/products/';
  
  describe('Products APIT,function(){
    /* Details of below data unnecessary to answer question */ 
    const validId = /* ID of product already in database */; 
    const invalidId = /* Invalid product ID */;
    const validProduct = {/* Valid product object*/};
  
    it('valid GET requests',function(done){
      request.get(apiRoot + validId).end(function(err,res){
        expect(res.statusCode).to.equal(status.OK);
        expect(res.body).to.have.property('price');
        expect(res.body).to.have.property('quantity'); 
        done();
      });
    });
  
    it('invalid GET requests',function(done){
      request.get(apiRoot + invalidId).end(function(err,res){
        expect(res.statusCode).to.equal(status.BAD_REQUEST);
        done();
      });
    });
  
    it('valid PUT requests',function(done){
      request.put(endpointURL + validId).send(validProduct)
      .end(function(err,res){
      expect(res.statusCode).to.equal(status.OK);
      expect(res.body).to.deep.equal(validProduct);
      request.get(apiRoot + validID).end(function(err,res){
        expect(res.body.price).to.equal(validProduct.price);
        expect(res.body.quantity)
          .to.equal(validProduct.quantity);
        done();
      });
    });
  });
  </code></pre>
  <p>The tests also partially specify the behaviour of the API when responding to a PUT request. It should be possible to update an existing product to take a new (valid) value by making a PUT request to the path /api/vl/products/ followed by the product ID (e.g. /api/vl/products/42 for the product with ID 42). The response to a successful update should have status code 400 (OK) and the body of the response should contain the updated product. Updating the product should not change its ID.
  Below is a partial implementation of the API using the express framework</p>
  <pre><code>var express = require('express');
  var app = express();
  var products = require('./lib/products');
  
  app.getC/api/v1/products/:idP,function(req,res,next){ 
    const id = req.params._id;
    /*i) Complete for question i) here */
  } ) ;
  app.putC/api/v1/products/:idP,function(req,res,next){ 
    const id = req.params._id;
    /*ii) Complete for question ii) here */
  } ) ;
  
  var port = 3000;
    app.listen(port,function(){
    console.log('Listening on port ' + port);
  });
  </code></pre>
  <p>where the products variable contains a data layer with the following methods:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th style="text-align: left;">Method Signature</th>
  <th style="text-align: center;">Description</th>
  <th style="text-align: right;">Result</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td style="text-align: left;">read(id,callback)</td>
  <td style="text-align: center;">reads product with</td>
  <td style="text-align: right;">Product</td>
  </tr>
  <tr>
  <td style="text-align: left;"></td>
  <td style="text-align: center;">specified id.</td>
  <td style="text-align: right;">with</td>
  </tr>
  <tr>
  <td style="text-align: left;"></td>
  <td style="text-align: center;"></td>
  <td style="text-align: right;">specified id</td>
  </tr>
  <tr>
  <td style="text-align: left;">---------------------------</td>
  <td style="text-align: center;">--------------------</td>
  <td style="text-align: right;">------------</td>
  </tr>
  <tr>
  <td style="text-align: left;">update(id,product,callback)</td>
  <td style="text-align: center;">updates product with</td>
  <td style="text-align: right;">updated</td>
  </tr>
  <tr>
  <td style="text-align: left;"></td>
  <td style="text-align: center;">specified id to have</td>
  <td style="text-align: right;">product.</td>
  </tr>
  <tr>
  <td style="text-align: left;"></td>
  <td style="text-align: center;">value of parameter</td>
  <td style="text-align: right;"></td>
  </tr>
  <tr>
  <td style="text-align: left;"></td>
  <td style="text-align: center;">product.</td>
  <td style="text-align: right;"></td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>In both of these cases, callback will be a function to be called after the database operation has completed (successfully or unsuccessfully) with two arguments. The first argument will be an error object (null if and only if the operation has completed without errors). The second argument will be a result object containing the result of a successfully completed operation as described in the table above.</p>
  <p>i)  Complete the code above such that the API responds to GET requests as specified in the tests.</p>
  <p>ii) Complete the code above such that the API responds to PUT requests as specified in the tests.</p>
  <p>I have placed my attempt at question i) here below:</p>
  <pre><code>product.read(id,function(err,res)){
      if(res.status(status.OK)
          return res.sendStatus(status.OK);
          return res.body('price' &amp;&amp; 'quantity');
      if(res.status(status.BAD_REQUEST))
          return res.sendStatus(status.BAD_REQUEST);
  }
  </code></pre>
  <p>but for ii) im afraid my brain just literally CRASHES, im new to node.js and js so please dumb down the explanation as much as you can for me!! Thank you i very much will appreciate all help since i am a beginner and im finding it hard to wrap my head around this question! Lecture notes do not seem to help much :((</p>