lab6_handout_COMP3322_s2018.pdf - COMP3322 Modern Technologies on World Wide Web Lab 6 RESTful Web Service Using Express.js MongoDB and Pug Introduction

lab6_handout_COMP3322_s2018.pdf - COMP3322 Modern...

This preview shows page 1 - 3 out of 12 pages.

COMP3322 Modern Technologies on World Wide Web Lab 6: RESTful Web Service Using Express.js, MongoDB and Pug Introduction In this lab exercise, we will use Node.js to implement a RESTful Web service and the HTML content to access the Web service. In particular, we will use the Express.js web framework based on Node.js, together with the Pug template engine and MongoDB. The Web service allows retrieving, adding, updating and deleting commodities from a MongoDB database. The HTML page provides an interface for displaying commodities, adding, updating and deleting them. Set Up Runtime Environment and Install MongoDB Follow the instructions (Steps 1 to 3) in setup_nodejs_runtime_and_examples.pdf to set up Node.js runtime environment and create an Express project named lab6 . Remember to replace app.set('view engine', 'jade'); in the generated app.js by app.set('view engine', 'pug'); . In addition, we will need a MongoDB database to store reports information. We install the database as follows. Step 1 : In the “lab6” project directory, create a new directory data ”. This directory will be used to store database files. Step 2 : Go to and download the latest version of MongoDB (choose the latest “Community Server” release to download) . If you use HW311/312’s 32-bit computer to complete this lab exercise, you should download the 32bit version here: ). Install MongoDB to a directory at your choice. Step 3 : Launch the 2nd terminal (besides the one you use for running NPM commands), and switch to the directory where MongoDB is installed. Start MongoDB server using the “data” directory of “lab6” project as the database location, as follows: (replace YourPath by the actual path on your computer that leads to “lab6” directory) If you use a 64-bit MongoDB on your own computer, please use the following command ./bin/mongod --dbpath YourPath /lab6/data If you use a 32-bit MongoDB, please use the following command instead: ./bin/mongod --storageEngine= mmapv1 --dbpath YourPath /lab6/data After starting the database server successfully, you should see some prompt in the terminal cd lab6 mkdir data
Image of page 1
like “I NETWORK [initandlisten] waiting for connections on port 27017”. This means that the database server is up running now and listening on the default port 27017. Then leave this terminal open and do not close it during your entire lab practice session, in order to allow connections to the database from your Express app. Step 4 : Launch the 3 rd terminal , switch to the directory where mongodb is installed, and execute the following commands: ./bin/mongo use lab6 db.commodities.insert({'category':'Computer','name':'lenovo','status':'in stock'}) The “use lab6” command creates a database named “lab6”. The next command followed by “use lab6” inserts a new document into the “ commodities ” collection in the database.
Image of page 2
Image of page 3

You've reached the end of your free preview.

Want to read all 12 pages?

  • Fall '13
  • Dr. C. Wu
  • World Wide Web, Hypertext Transfer Protocol, HTTP

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture