User Tools

Site Tools


njsetn:serving_json_data
var http = require("http");

var data = require("./data/inventory");

http.createServer(function(req, res) {

  if (req.url === "/") {
    res.writeHead(200, {"Content-Type": "text/json"});
    res.end(JSON.stringify(data));
  } else if (req.url === "/instock") {
    listInStock(res);
  } else if (req.url === "/onorder") {
    listOnOrder(res);
  } else {
    res.writeHead(404, {"Content-Type": "text/plain"});
    res.end("Whoops... Data not found");
  }

}).listen(3000);

function listInStock(res) {

  var inStock = data.filter(function(item) {
    return item.avail === "In stock";
  });
  
  res.end(JSON.stringify(inStock));
  
}
function listOnBackOrder(res) {

  var onOrder = data.filter(function(item) {
    return item.avail === "On back order";
  });
  
  res.end(JSON.stringify(onOrder));
  
}
njsetn/serving_json_data.txt · Last modified: 2016/03/25 20:56 by leo