1. Homepage
  2. Programming
  3. COMP3322 Modern Technologies on World Wide Web - Assignment Four: Big Cities

COMP3322 Modern Technologies on World Wide Web - Assignment Four: Big Cities

Engage in a Conversation
COMP3322Modern Technologies on World Wide WebHKUNodeJSJavascriptMongoDB

COMP3322 Modern Technologies on World Wide Web Assignment Four
Total 12 points
CourseNana.COM

Deadline: 23:59 December 3, 2023 CourseNana.COM

Overview CourseNana.COM

Write an express.js program and name it index.js. This program provides the API to get data about big cities from a MongoDB server. CourseNana.COM

Objectives CourseNana.COM

  1. A learning activity to support ILO 1 and ILO 2. CourseNana.COM

  2. To practice how to use Node, Express, MongoDB, and Mongoose to create a simple REST API. CourseNana.COM

Specification CourseNana.COM

Assume you are using the MongoDB server running on the course’s node.js docker container with the service name mongodb listening to the default port 27017. CourseNana.COM

The database is named "bigcities" and it contains a collection called "cities". The collection consists of 34800 cities with a population of at least 10000. The data for this dataset is sourced from the GeoNames geographical database (https://www.geonames.org/about.html). Each record in the collection consists of 9 fields: _id, Name, “ASCII Name”, “ISO Alpha-2”, “ISO Name EN”, Population, Timezone, “Modification date”, and Coordinates. CourseNana.COM

ISO Alpha-2 CourseNana.COM

The ISO 3166 Alpha-2 country code
AR
CourseNana.COM

HK CourseNana.COM

Coordinates CourseNana.COM

The latitude and longitude values of the city -32.81636, -61.39493 22.27832, 114.17469 CourseNana.COM

You will be using the provided framework for developing your program. You can download the template file (template.txt) from the course’s Moodle site. CourseNana.COM

index.js CourseNana.COM

Example 1 Example 2 CourseNana.COM

_id CourseNana.COM

The id of record in Geonames database 3862981 CourseNana.COM

1819729 CourseNana.COM

Population CourseNana.COM

The population of the city CourseNana.COM

    36000
   7482500

Name CourseNana.COM

The name of the city (in UTF8) CourseNana.COM

Cañada de Gómez Hong Kong Timezone
The IANA timezone CourseNana.COM

ID CourseNana.COM

ASCII Name CourseNana.COM

The name of the city (in ASCII) Canada de Gomez Hong Kong CourseNana.COM

Modification date The date of last modification 2020-06-10 CourseNana.COM

ISO Name EN CourseNana.COM

The English name of the Alpha-2 code Argentina CourseNana.COM

Hong Kong, China CourseNana.COM

Example 1 Example 2 CourseNana.COM

America/Argentina/ Cordoba Asia/Hong Kong CourseNana.COM

2021-09-09
Download the big cities dataset (bigcities.csv) from the course’s Moodle site. Import the data to the CourseNana.COM

bigcities database for the tests. CourseNana.COM

const express = require('express')
const app = express();
/* Implement the logic here */
// error handler
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.json({'error': err.message});
});
app.listen(3000, () => {
  console.log('Weather app listening on port 8000!')

}); CourseNana.COM

TASK A CourseNana.COM

Use the command mongoimport to import the CSV file to the MongoDB server. Here are the steps to import the data to your docker’s mongodb server. CourseNana.COM

  1. Use Windows Explorer or Mac Finder to go to the data/db folder (which is inside the Node- dev folder). CourseNana.COM

  2. Copy the bigcities.csv file there. CourseNana.COM

  3. Access the docker desktop and open a terminal for the c33322-mongo container. CourseNana.COM

  4. In the terminal, type this command (in one line): CourseNana.COM

mongoimport -d=bigcities -c=cities --type=csv --headerline --columnsHaveTypes --file=bigcities.csv

Write the code to set up a connection to the MongoDB server using Mongoose. Use the following schema to access the database. CourseNana.COM

     Schema {
       Name: String,
       'ASCII Name': String,
       'ISO Alpha-2': String,
       'ISO Name EN': String,
       Population: Number,
       Timezone: String,
       'Modification date': String,
       Coordinates: String

} CourseNana.COM

Write the code that monitors the database connection and terminates the program if the connection to the database is lost. CourseNana.COM

Write a routing endpoint to handle all GET requests to the URL http://localhost:3000/cities/v1/all?gte=xxxxx&lte=yyyyy CourseNana.COM

for retrieving the entire big cities dataset or a portion of the dataset based on the population range defined in the query string. The server should respond with a JSON message and an appropriate HTTP status code that reflects the completion status of the GET request to the client. CourseNana.COM

Situations: CourseNana.COM

1. GET /cities/v1/all
When the GET request is made without a query string, the program retrieves the entire dataset from the database. It then
converts the Coordinates field to an object with two properties: ‘lat’ and ‘lng’. These properties represent the latitude and longitude values (both of type Number) of the city. The program returns the entire dataset in JSON format to the client with the HTTP status code 200. The returned JSON message is an array that contains all the documents, ordered by the _id field. CourseNana.COM

2. GET /cities/v1/all?gte=xxxxx
GET /cites/v1/all?lte=yyyyy
GET /cities/v1/all?gte=xxxxx&lte=yyyyy
When the GET request includes a query string
with the ‘gte’ and/or ‘lte’ parameters, the program retrieves the dataset from the database based on the population range specified by the query string. ‘gte’ stands for and ‘lte’ stands for . For example, the program retrieves all cities with a population one million for the parameter gte=1000000. Another example, the program retrieves all cities with a population between 500000 x 1000000 for the parameters gte=500000&lte=1000000. After retrieving the dataset, the program should convert the Coordinates field to an object and sort the dataset in descending order of population. The program then returns the dataset in JSON format to the client with HTTP status code 200. CourseNana.COM

The program should return a JSON string '{"error":"No record for this population range"}' with the HTTP status code 404 when it could not find any documents matching the limit defined by the parameters, e.g., lte=1000&gte=10000. CourseNana.COM

3. When the program experiences an error (e.g., database issue), it returns the HTTP status code 500 with a JSON string '{"error":$message}', where $message stands for the error message of that error event. CourseNana.COM

TASK C CourseNana.COM

Create a routing endpoint that handles all GET requests to the URLs http://localhost:3000/cities/v1/alpha CourseNana.COM

http://localhost:3000/cities/v1/alpha/{code} CourseNana.COM

for retrieving all the alpha codes in the dataset or all the documents in the dataset that match a specified alpha code in the URL path. The server should respond with a JSON message and the appropriate HTTP status code to indicate the completion status of the GET request. CourseNana.COM

Situations: CourseNana.COM

  1. /cities/v1/alpha
    With this GET request, the program searches the database to find all unique alpha-2 codes in the dataset. For each alpha-2 code, the program creates
    an object with two properties: 'code' and 'name', which contain the values from the ISO Alpha-2 and ISO Name EN fields, respectively. The program then groups all alpha-2 code objects into an array and sorts them in ascending order based on the alpha-2 codes. Finally, the program returns this array object as a JSON message to the client with a status code of 200. CourseNana.COM

  2. /cities/v1/alpha/{code}
    With this GET request, the program searches the database to retrieve all documents that match the specified alpha code in the path. For example, if the requested path is '/cities/v1/alpha/HK', the program will find all documents with the 'HK' alpha-2 code. For each matched document, the program retrieves the following fields:
    “ASCII Name”, Population, Timezone, and Coordinates. It converts the Coordinates field to an object and groups all matched documents in descending order based on population. The program then returns this array object as a JSON message to the client with status code 200. CourseNana.COM

    The program should return a JSON string {error:No record for this alpha code}with the HTTP status code 404 when it could not find any documents matching the requested alpha code. CourseNana.COM

  3. When the program experiences an error (e.g., database issue), it returns the HTTP status code 500 with a JSON string {error:$message}, where $message stands for the error message of that error event. CourseNana.COM

TASK D CourseNana.COM

Create a routing endpoint that handles all GET requests to the URLs CourseNana.COM

http://localhost:3000/cities/v1/region http://localhost:3000/cities/v1/region/{region} CourseNana.COM

for retrieving all the regions in the dataset or all the documents in the dataset that match a specified region in the URL path. In response, the server returns a JSON message and appropriate HTTP status code to the client, which reflects the completion status of the GET request. CourseNana.COM

Situations: CourseNana.COM

  1. /cities/v1/region
    With this request, the program retrieves the Timezone field of all documents and extracts the first component of the Timezone field to be the region. For example, if the Timezone value is "America/Argentina/Cordoba", the program will extract the region as "America". The program then returns all unique regions in the dataset as a JSON message to the client with the HTTP status code 200. The JSON message lists all regions
    in alphabetical order. CourseNana.COM

  2. /cities/v1/region/{region}
    With this GET request, the program searches the database to retrieve all documents that have the first component of the Timezone field matches the specified region in the URL path. For example, if the requested path is '/cities/v1/region/Atlantic', the program will find 72 documents. For each matched document, the program retrieves only
    the following fields: “ASCII Name”, “ISO Alpha-2”, “ISO Name EN”, Population, Timezone, and Coordinates. It converts the Coordinates field to an object and groups all matched documents in descending order based on population. The program then returns this array object as a JSON message to the client with status code 200. CourseNana.COM

    The program should return a JSON string {error:No record for this region”}with CourseNana.COM

    the HTTP status code 404 when it could not find any documents matching the requested region. CourseNana.COM

  3. When the program experiences an error (e.g., database issue), it returns the HTTP status code CourseNana.COM

    500 with a JSON string {error:$message}, where $message stands for the error message of that error event. CourseNana.COM

TASK E CourseNana.COM

Create a routing endpoint that handles all GET requests to the URL http://localhost:3000/cities/v1/{city}?partial=true&alpha=xx&region=yyyy&sort=alpha|pop CourseNana.COM

ulation CourseNana.COM

for retrieving all the documents in the dataset that match the specified city in the URL path. In response, the server returns a JSON message and appropriate HTTP status code to the client, which reflects the completion status of the GET request. CourseNana.COM

Situations:
1. /cities/v1/{city}
CourseNana.COM

With this GET request, the program retrieves all documents in the database that have the “ASCII Name” field exactly matches with the specified city name in the URL path. For example, when the city name is “Logan”, the program returns only one document; whereas for the city name “Paris”, it returns 4 matched documents. For each matched document, the program retrieves the following fields only: _id, “ASCII Name”, “ISO Alpha-2”, “ISO Name EN”, Population, Timezone, and Coordinates. It converts the Coordinates field to an object and groups all matched documents in ascending order based on the _id field. The program then returns this array object as a JSON message to the client with status code 200. CourseNana.COM

  1. /cities/v1/{city}?partial=true
    When a query string is provided with the parameter
    “partial=true”, the program finds all documents where the “ASCII Name” field partially matches with the specified city name in the URL path. For example, when the city name is “Logan”, the program returns 6 matched documents that have the string “Logan” in their “ASCII Name” fields. If the parameter “partial” has a value other than “true”, the program should ignore this parameter and apply the exactly match as the searching criteria. CourseNana.COM

  2. /cities/v1/{city}?alpha=xx
    /cities/v1/{city}?region=yyyy
    When the query string contains the
    “alpha” parameter, the
    program restricts the search to documents under this alpha
    code for the exactly or partially matched of the city name
    (based on the partial parameter). For example, if a search is performed on the city name "Logan" with partial=true and alpha=AU, only one matched city is found.
    When the query string contains
    the “region” parameter, the program restricts the search to documents under this region for the exactly or partially matched of the city name. For example, when searching for the city name “Logan” with partial=true and region=America, five matched cities are located.
    If both the alpha and region parameters are provided, the program should ignore the region parameter as the alpha parameter should have a higher priority.
    CourseNana.COM

  3. /cities/v1/{city}?sort=alpha|population
    If the sort parameter is not included, the default order will be based on the ascending order of the _id field. If the sort parameter is included with
    the value “alpha”, all returned results will be sorted in ascending order of the alpha code. If the sort parameter is included with the value “population”, all returned results will be sorted in the descending order of population. Otherwise, ignore other values and use the default order. CourseNana.COM

  4. The program should return a JSON string {error:No record for this city name}with the HTTP status code 404 when it could not find any documents matching the requested city name with the parameters. CourseNana.COM

  5. When the program experiences an error (e.g., database issue), it returns the HTTP status code 500 with a JSON string {error:$message}, where $message stands for the error message of that error event. CourseNana.COM

Write a routing endpoint to intercept all other request types and paths, which are not defined in previous tasks. Return a JSON string with the HTTP status code 400. For example, for the request POST /cities/v1/all HTTP/1.1, we get the response '{"error":"Cannot POST /cities/v1/all"}'; for the request GET /cities/alpha/AU HTTP/1.1, we get the response '{"error":"Cannot GET /cities/alpha/AU"}'. CourseNana.COM

Resources CourseNana.COM

You are provided with the following files. CourseNana.COM

1. A JSON file use mongoexport to export the whole collection from the bigcities database. CourseNana.COM

Similar to the mongoimport command, you have to open a terminal at the data/db folder CourseNana.COM

and type the following command (in one line): CourseNana.COM

mongoexport -d=bigcities -c=cities --jsonArray --sort='{_id: 1}' --out=3035111999.json

Replace 3035111999 with your student ID and upload this JSON file. CourseNana.COM

  1. The complete index.js program and other required files. CourseNana.COM

  2. The package.json file of your express program. CourseNana.COM

Grading Policy CourseNana.COM

Points Criteria CourseNana.COM

Task A
Database set up, import the data set, and export the data set.
The program can connect and access the MongoDB database.
The program can detect that the database connection is broken. CourseNana.COM

Task B
Correctly handle the GET request to return all big cities data
Correctly handle the GET request with query string parameters to return the CourseNana.COM

specific set of data Error handling CourseNana.COM

Task C CourseNana.COM

Task D CourseNana.COM

CourseNana.COM

Task E CourseNana.COM

  • ▪  Correctly handle the GET request to retrieve all cities that have the matched city CourseNana.COM

    name or partially matched city name. CourseNana.COM

  • ▪  Correctly handle the GET request to retrieve all cities that have the matched city CourseNana.COM

    name or partially matched city name within a specific region/alpha-2 code and CourseNana.COM

    sorting order. CourseNana.COM

  • ▪  Error handling CourseNana.COM

1.0 Task F
Error handling of all unknown methods and paths CourseNana.COM

-4.0 Using any external libraries. CourseNana.COM

Plagiarism CourseNana.COM

Plagiarism is a very serious academic offence. Students should understand what constitutes plagiarism, the consequences of committing an offence of plagiarism, and how to avoid it. Please note that we may request you to explain to us how your program is functioning as well as we may also make use of software tools to detect software plagiarism.  CourseNana.COM

Get in Touch with Our Experts

WeChat WeChat
Whatsapp WhatsApp
COMP3322代写,Modern Technologies on World Wide Web代写,HKU代写,NodeJS代写,Javascript代写,MongoDB代写,COMP3322代编,Modern Technologies on World Wide Web代编,HKU代编,NodeJS代编,Javascript代编,MongoDB代编,COMP3322代考,Modern Technologies on World Wide Web代考,HKU代考,NodeJS代考,Javascript代考,MongoDB代考,COMP3322help,Modern Technologies on World Wide Webhelp,HKUhelp,NodeJShelp,Javascripthelp,MongoDBhelp,COMP3322作业代写,Modern Technologies on World Wide Web作业代写,HKU作业代写,NodeJS作业代写,Javascript作业代写,MongoDB作业代写,COMP3322编程代写,Modern Technologies on World Wide Web编程代写,HKU编程代写,NodeJS编程代写,Javascript编程代写,MongoDB编程代写,COMP3322programming help,Modern Technologies on World Wide Webprogramming help,HKUprogramming help,NodeJSprogramming help,Javascriptprogramming help,MongoDBprogramming help,COMP3322assignment help,Modern Technologies on World Wide Webassignment help,HKUassignment help,NodeJSassignment help,Javascriptassignment help,MongoDBassignment help,COMP3322solution,Modern Technologies on World Wide Websolution,HKUsolution,NodeJSsolution,Javascriptsolution,MongoDBsolution,