// DO NOT CHANGE! //init app with express, util, body-parser, csv2json var express = require('express'); var app = express(); var sys = require('util'); var path = require('path'); var bodyParser = require('body-parser'); var Converter = require("csvtojson").Converter; //register body-parser to handle json from res / req app.use(bodyParser.json()); //register public dir to serve static files (html, css, js) app.use(express.static(path.join(__dirname, "public"))); // END DO NOT CHANGE! /************************************************************************** ****************************** csv2json ********************************* **************************************************************************/ const csv = require('csvtojson') var json; csv() .fromFile("world_data.csv") .then((jsonObj) => { json = jsonObj; }) /************************************************************************** ********************** handle HTTP METHODS *********************** **************************************************************************/ // ------------------------------ GET ------------------------------ app.get("/items", function (req, res) { console.log(json); res.send(json); }); app.get("/items/:id", function (req, res) { var requested_id = parseInt(req.params.id); var answer = ["Err", "No such id " + requested_id + " in database"]; for (var i = 0; i < json.length; i++) { if (json[i].id == requested_id) { answer = [json[i]]; } } res.send(answer); }); app.get("/items/:id1/:id2", function (req, res) { var first_requested_id = parseInt(req.params.id1); var second_requested_id = parseInt(req.params.id2); var answer = ["Err", "Range not possible"]; // check that first id is bigger than 0 // and the second one is the bigger one if (first_requested_id >= 0 && second_requested_id > first_requested_id) { var enable_push = false; for (var i; i < json.length; i++) { // enable push when the first id is found if (parseInt(json[i].id) == first_requested_id) { enable_push = true; } // push key when enabled if (enable_push) { answer.push(json[i]); } // break when second id is found if (parseInt(json[i].id) == second_requested_id) { break; } } } res.send(answer); }); app.get("/properties", function (req, res) { var answer = []; for (var key in json[0]) { answer.push(key); } res.send(answer); }); app.get("/properties/:num", function (req, res) { let num = parseInt(req.params.num); var answer = ["Err", "No such property available"]; var i = 0; for (var key in json[0]) { if (i == num) { answer = [key]; break; } i++; } res.send(answer); }) // ----------------------------- POST ------------------------------ app.post("/items", function (req, res) { var new_entry = req.body; // set id var last_id = parseInt(json[json.length - 1].id); new_entry["id"] = (last_id + 1).toString(); // add entry to json json.push(new_entry); res.send("Added country " + new_entry.name + " to list!"); }); // ---------------------------- DELETE ----------------------------- app.delete("/items", function (req, res) { var name = json[json.length - 1].name; json.splice(-1, 1); res.send("Deleted last country " + name + "!"); }); app.delete("/items/id", function (req, res) { var requested_id = parseInt(req.params.id); var answer = ["Err", "No such id " + requested_id + "in database"]; for (var i = 0; i < json.length; i++) { if (json[i].id == requested_id) { json.splice(i, 1); answer = ["Item " + requested_id + " deleted successfully."]; break; } } res.send(answer); }); // DO NOT CHANGE! // bind server to port var server = app.listen(3000, function () { var host = server.address().address; var port = server.address().port; console.log('Example app listening at http://%s:%s', host, port); });