CSCI 2006 - Spring 2024 - Server-Side ProgrammingLab #6 - jsNode

Lab #6 - jsNode

Purpose: Implement a Data Source for AJAX requests

Instructions

  1. This lab will be implementing an API for some website's AJAX requests. As such, we will not be producing HTML output, but instead JSON output
  2. This lab will require SSH sessions (the recommended application for Windows is PuTTy), please reach out to the instructor if you need assistance with that
  3. This particular lab will require more sharing between all students. While "running" your solution, other students will not be able to "run" their solutions. This may not end up being a problem, as long as everyone does not attempt to test their solutions at the same time
  4. Create a lab06 folder in your csci2006 folder
  5. Run the following command:
    npm init -y & npm install express
  6. Create a file called "app.js", and put the following skeleton code in it
    const express = require('express')
    const app = express()
    
    dataset = {
            106020: {
                    'name':'Girl with a Pearl Earring',
                    'artist':'Vermeer',
                    'date':1665,
                    'desc':'Girl with a Pearl Earring is an oil painting by Dutch Golden Age painter Johannes Vermeer, dated c. 1665. Going by various names over the centuries, it became known by its present title towards the end of the 20th century after the earring worn by the girl portrayed there.',
                    'variations': [
                            {
                                    'name':'4x5 Reprint',
                                    'price': 40,
                            },
                            {
                                    'name':'8x10 Reprint',
                                    'price': 80,
                            },
                            {
                                    'name':'12x15 Reprint',
                                    'price': 120,
                            }
                    ]
            },
            116010: {
                    'name':'Artist Holding a Thistle',
                    'artist':'Dürer',
                    'date':1493,
                    'desc':'Portrait of the Artist Holding a Thistle is an oil painting on parchment pasted on canvas by German artist Albrecht Dürer. Painted in 1493, it is the earliest of Dürer\'s painted self-portraits and has been identified as one of the first self-portraits painted by a Northern artist.',
                    'variations': [
                            {
                                    'name':'4x5 Reprint',
                                    'price': 65,
                            },
                            {
                                    'name':'8x10 Reprint',
                                    'price': 125,
                            },
                            {
                                    'name':'12x15 Reprint',
                                    'price': 180,
                            }
                    ]
            },
            120010: {
                    'name':'Portrait of Eleanor of Toledo',
                    'artist':'Cosimo',
                    'date':1545,
                    'desc':'The Portrait of Eleanor of Toledo and Her Son is a painting by the Italian artist Agnolo di Cosimo, known as Bronzino, finished ca. 1545. One of his most famous works, it is housed in the Uffizi Gallery of Florence, Italy and is considered one of the preeminent examples of Mannerist portraiture',
                    'variations': [
                            {
                                    'name':'4x5 Reprint',
                                    'price': 50,
                            },
                            {
                                    'name':'8x10 Reprint',
                                    'price': 75,
                            },
                            {
                                    'name':'12x15 Reprint',
                                    'price': 125,
                            }
                    ]
            }
    }
    
    app.get('/[username]/artists', (req, res) => {
            artists = {};
            for (const [key,value] of Object.entries(dataset)) {
                    if (value.artist in artists) {
                            artists[value.artist].push(key);
                    } else {
                            artists[value.artist] = [key];
                    }
            }
            res.send(JSON.stringify(artists));
    });
    
    app.listen(8080, () => {
                console.log('listening on http://localhost:8080');
    });
  7. Replace the [username] with your username (e.g. for myself, it would become app.get('/matt/artists', ...)
  8. To run this code, in a SSH session, navigate to your lab06 folder, and execute "node app.js". If you do not get an error about "address already in use :::8080", then you can go to a browser and navigate to 74.208.52.43:8080/[username]/artists
  9. You should see
    {"Vermeer":["106020"],"Dürer":["116010"],"Cosimo":["120010"]}
  10. Add a route for /[username]/artworks that will display an object of the id and artwork names. The results should look like
    {"106020":"Girl with a Pearl Earring","116010":"Artist Holding a Thistle","120010":"Portrait of Eleanor of Toledo"}
  11. Add a route for /[username]/products that will display an array of objects, where each object represents a single variation and contains the artwork id, the name of the artwork, variation name, and variation price. The results should look like
    [{"id":"106020","name":"Girl with a Pearl Earring","var":"4x5 Reprint","price":40},{"id":"106020","name":"Girl with a Pearl Earring","var":"8x10 Reprint","price":80},{"id":"106020","name":"Girl with a Pearl Earring","var":"12x15 Reprint","price":120},{"id":"116010","name":"Artist Holding a Thistle","var":"4x5 Reprint","price":65},{"id":"116010","name":"Artist Holding a Thistle","var":"8x10 Reprint","price":125},{"id":"116010","name":"Artist Holding a Thistle","var":"12x15 Reprint","price":180},{"id":"120010","name":"Portrait of Eleanor of Toledo","var":"4x5 Reprint","price":50},{"id":"120010","name":"Portrait of Eleanor of Toledo","var":"8x10 Reprint","price":75},{"id":"120010","name":"Portrait of Eleanor of Toledo","var":"12x15 Reprint","price":125}]
  12. Add a route for /[username]/artwork/[artwork-id] that will display the information related to the requested artwork. This route will require you to use a URL parameter. The results for /[username]/artwork/106020 shoudl look like
    {"name":"Girl with a Pearl Earring","artist":"Vermeer","date":1665,"desc":"Girl with a Pearl Earring is an oil painting by Dutch Golden Age painter Johannes Vermeer, dated c. 1665. Going by various names over the centuries, it became known by its present title towards the end of the 20th century after the earring worn by the girl portrayed there.","variations":[{"name":"4x5 Reprint","price":40},{"name":"8x10 Reprint","price":80},{"name":"12x15 Reprint","price":120}]}
  13. Modify the route for artworks so that there is an optional /[artist-name] parameter. If the parameter is blank there should be no change to the output from before. If the parameter has a name, it should filter the results to just those artworks by that artist
  14. Modify the route for products so that there is an optional /[artist-name] parameter. If the parameter is blank there should be no change to the output from before. If the parameter has a name, it should filter the product list to just those that are by that artist

Submitting Instructions