0% found this document useful (0 votes)
18 views2 pages

COMP5347 - W10 - Restful Service - 2020

This tutorial document discusses creating and consuming a RESTful service in Node.js. It includes 3 parts: 1) adding the request module, 2) creating a RESTful API using MongoDB to query and return revision data as JSON, and 3) consuming the REST service by implementing a parser to output status and parse/count unique users from the JSON response.

Uploaded by

Destrious
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views2 pages

COMP5347 - W10 - Restful Service - 2020

This tutorial document discusses creating and consuming a RESTful service in Node.js. It includes 3 parts: 1) adding the request module, 2) creating a RESTful API using MongoDB to query and return revision data as JSON, and 3) consuming the REST service by implementing a parser to output status and parse/count unique users from the JSON response.

Uploaded by

Destrious
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

COMP5347 Tutorial

School of Computer Science

COMP5347: Web Application Development


Week 10 Tutorial: Restful Service

Learning Objectives
• Understand how to create and consume restful service in Node.js

Part 1: Add request module


Task: Download the provided week10-src.zip from Canvas and extract the content in
a directory. Start Eclipse, select “open projects from file system” under file menu, go to
the directory you just extracted the content and click open. In this case, all the
necessary node modules are specified in package.json. Right click it on the project
explorer panel, select Run as then npm install.
Optional: Just keep in mind, in your own group assignment, you need to run the
following command in the terminal to install “request” manually.
npm install request --save
Save the file and right click it on the project explorer panel to Run As then npm update.

1|Page
COMP5347 Tutorial

Part 2: Create Restful API

Task: Start the mongodb server using the following command, replace the “path” with a
path to the local mongodb working directory where you create and use in Week 7 to
Week 9 (the wikipedia database with the revisions collections).
mongod --dbpath path

Right click reverser.js on the explorer panel and Run as Node.js application.
Open the browser and go to the address below. You should be able to see something
looks like the screenshot below.
http://localhost:3000/revision/collection/BBC

Check RevisionSchema.statics.getByTitle in app/models/revisions.js. A


simple callback function is defined here to query to database with given article title. In
app/controllers/revision.server.controller.js, a controller is defined to
return the query results as JSON objects. Overall, a REST API is created that accepts
GET request with given article title and returns the results in JSON format.

Part 3: Consume REST Service


In this part, you are required to implement a parser which can parse the JSON objects
returned from REST API and output the associated results in console. After implemented,
when you run request.js as Node application, it should print like this in the console:

Now please implement the parse which will check if the URL (Wikipedia API) is available,
output the service status in console. If it is available, please parse the returned JSON
objects and output how many unique users have made revisions to article “CNN”.
2|Page

You might also like