In this exercise, you will get started with a simple node application. The application will read from two files, one JSON file, and one XML file.
The files contain famous movie titles and some metadata about those movies. You are supposed to write an application that reads the data from the files and calculates the average rating of the movies. The data in the JSON file stems from IMDB, whereas the XML file stems from Rotten Tomatoes. One average is calculated for the IMDB data and one average for the Rotten Tomatoes data. (The focus of this exercise is promises and callbacks, not calculating stuff.)
Let's get started!
Before you begin, make sure you have a laboratory environment set up according to your course specification.
-
Start by creating a
package.json
identifying your project. (npm init
). -
Follow the instructions at https://www.npmjs.com/package/@lnu/eslint-config to config the linting tool for the code and JSDoc comments.
-
Add
start
,lint
andlint:fix
scripts to thepackage.json
file."scripts": { "start": "node app.js", "lint": "npx eslint . || exit 0", "lint:fix": "npx eslint . --fix || exit 0" }
-
Save the
package.json
file and runnpm install
. -
Create a
.ignore
file for your environment (git ignore node,visualstudiocode,windows >> .gitignore
, if you created the ignore alias, if necessary replacevisualstudiocode
with your IDE and/orwindows
withmacos
orlinux
). -
Create an
app.js
file in the root directory. (touch app.js
) -
Create a directory called
lib
. (mkdir lib
) -
Create a directory in the
lib
directory calledmovies
(mkdir lib/movies
) -
Create a file called
reviewer.js
in thelib
directory (touch lib/reviewer.js
) -
Export a function from
reviewer.js
and require it inapp.js
. Addconsole.log('Hello, World!')
to the function inreviewer.js
. Call the function fromapp.js
and run the application usingnpm start
. If the console greets you with "Hello, World!" you are good to go. Otherwise, debug! -
Copy the JSON file and the XML file into the folder
./lib/movies
:wget -O ./lib/movies/movies.xml 'https://raw.githubusercontent.com/CS-LNU-Learning-Objects/the-node-platform-exercise-rotten-tomatoes/master/movies.xml'
wget -O ./lib/movies/movies.json 'https://raw.githubusercontent.com/CS-LNU-Learning-Objects/the-node-platform-exercise-rotten-tomatoes/master/movies.json'
-
Find a package at npm that can convert xml to JavaScript objects. (xml2js)
-
Add the package to your project. (
npm install xml2js
)
Now you are good to go.
In this exercise, the main goal is to train your skills in handling callbacks and promises. The recommended path to follow is, therefore:
- Solve the exercise using the
fs
module (const fs = require('fs')
) to read the content of the files and the modulexml2js
to convert the XML content to a JavaScript object. - Wrap the fs callback interface behind a promise module, i.e., create a module called
fs-promise.js
with functions returning promises. Make use ofPromise.all()
to be able to read the XML and JSON files in parallel. - Search the npm library and discover that someone else already made a promise wrapper. Remove your
fs-promise.js
and usefs-extra
instead. (npm install fs-extra
const fs = require('fs-extra')
) - Use async/await and promises for even slicker program flow.
$ npm start
Average rating
IMDB: 9.08
Rotten Tomatoes: 94.8 %
Using the built-in time measurement tool console.time('What I am measuring')
and corresponding console.timeEnd('What I am measuring')
you can measure the execution time of parts of your code. Use that to analyze which parts of your code are the bottlenecks. How could that be avoided?
https://github.com/CS-LNU-Learning-Objects/SOLUTION-the-node-platform-exercise-rotten-tomatoes