From 706d451c9244ea73421ac07740bcacebe1aa77fa Mon Sep 17 00:00:00 2001 From: "Mark Tyers (aa7401)" Date: Sun, 2 Jul 2017 13:58:18 +0100 Subject: [PATCH 1/3] added more notes on nvm --- 00 Introduction.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/00 Introduction.md b/00 Introduction.md index d6719e8..9aa0cb3 100644 --- a/00 Introduction.md +++ b/00 Introduction.md @@ -96,6 +96,32 @@ $ node -v v8.1.0 ``` +Over time you will update the version of NodeJS and, as a result will end up with multiple versions installed on your computer. You can check which version are currently install using. +``` +$ nvm ls + v5.1.0 + -> v7.3.0 + v8.1.2 + system + default -> 8.1.2 (-> v8.1.2) + node -> stable (-> v8.1.2) (default) + stable -> 8.1 (-> v8.1.2) (default) + iojs -> N/A (default) +``` +As you can see there are three versions of nodejs installed and the one currently in use is v7.3.0.We can switch to a different version with `nvm use 5.1.0` for example. In the example above we are using v7.3.0 with the default is set to 8.1.2. The default version can be set using. +``` +$ nvm alias default 8.1.2 +``` +If we no longer need one of the installed versions it can be removed. +``` +$ nvm uninstall 5.1.0 + Uninstalled node v5.1.0 +``` +When you update your node version you will also need to update any packages to run under the new version. +``` +$ nvm reinstall-packages 8.1.2 +``` + ## Installing a Code Editor There are plenty of choices when it comes to editors. The examples in this book will use the [Visual Studio Code](https://code.visualstudio.com) editor which is available for all major platforms. If you are looking at other editors then choose one that supports JavaScript and plugins. From 6ffdad729c5194a3f4ee9112af92740088cbd919 Mon Sep 17 00:00:00 2001 From: "Mark Tyers (aa7401)" Date: Sun, 2 Jul 2017 14:19:59 +0100 Subject: [PATCH 2/3] starting the API Added detailed instructions on cloning and running the bookshop API. --- 08 Understanding APIs.md | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/08 Understanding APIs.md b/08 Understanding APIs.md index 1ace4c9..0522e46 100644 --- a/08 Understanding APIs.md +++ b/08 Understanding APIs.md @@ -3,7 +3,16 @@ The aim of this book is to teach you the principles and practical skills to build useful secure APIs but before we dive into the technical aspects we should take a step back and understand their purpose and how they should work. This chapter is slightly different from the others insofar as you will learn, not how to build an API but _why_ these are written and how they are used. The understanding you gain from this chapter will then help you design and build your own. -## 1 Working with the HTTP Protocol +## 1 Configuring the Sample API + +As you explore the features of a RESTful API you will be interacting with the **Bookshop API** which can be found at https://github.coventry.ac.uk/304CEM-1718SEPJAN/bookshop and will need to be downloaded and run on your computer. The following instructions show how to do this on an Ubuntu computer, you will need to adapt these if you are using a different operating system. It is assumed you have already installed a running version of MongoDB. If you have not yet done this, refer to chapter 7 (Data Persistence). + +1. Start by cloning the repository into you Documents directory `git clone https://github.coventry.ac.uk/304CEM-1718SEPJAN/bookshop ~/Documents/bookshop`. This will create a `bookshop/` directory which you will need to navigate to using your terminal. +2. The API can run in one of two modes, `development` or `production`, and this is determined by the `NODE_ENV` environment variable. Set this to `development`. +3. Now you can start the API by running the `index.js` script. + + +## 2 Working with the HTTP Protocol HTTP is an application protocol designed to support distributed hypermedia systems and is used when developing APIs so it is important that you have a good understanding of this. @@ -13,16 +22,16 @@ HTTP Headers allow the client to pass additional information with the request an User agent -### 1.1 The Request +### 2.1 The Request Request headers -### 1.2 The Response +### 2.2 The Response Response headers -### 1.3 The User Agent +### 2.3 The User Agent xxx From 2efe70741686d9fd31a1d45923223302f6c67145 Mon Sep 17 00:00:00 2001 From: Mark Tyers Date: Mon, 3 Jul 2017 10:53:23 +0100 Subject: [PATCH 3/3] installing mongoDB --- 00 Introduction.md | 7 +++++ 07 Data Persistence.md | 59 ++++++++++++++++++++++++++++-------------- 2 files changed, 47 insertions(+), 19 deletions(-) diff --git a/00 Introduction.md b/00 Introduction.md index d6719e8..5eb5537 100644 --- a/00 Introduction.md +++ b/00 Introduction.md @@ -115,3 +115,10 @@ Close the terminal and re-launch. Now you can open any directory. - `code ~/Documents/bookshop/` opens Visual Studio Code with the contents of the directory specified. - `code .` opens Visual Studio Code with the contents of the _current directory_. + +### Using a Mac for Development + +Computers running MacOS are popular with NodeJS developers. MacOS is a flavour of Unix just like Linux and so all of the commands used in this book will work just fine. The biggest issue is the lack of a decent package manager such as the `apt` package manager found on Ubuntu. To get round this you should install the [Homebrew](https://brew.sh) package manager. +``` +/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" +``` diff --git a/07 Data Persistence.md b/07 Data Persistence.md index ba84bb9..09cc46e 100644 --- a/07 Data Persistence.md +++ b/07 Data Persistence.md @@ -44,34 +44,32 @@ exports.addBook = function(isbn, callback) { } ``` -## Relational Database - -Familiar data storage +## 2 Document Database -Supports complex queries +Stores JavaScript objects -Difficult to scale +Can be queried using JavaScript -Data needs normalising -```javascript -const mysql = require('mysql') -const request = require('request') +### 2.1 Installing MongoDB -const connection = mysql.createConnection({ host: 'xxx', port: '3306', user: 'xxx', password: 'xxx', database: 'xxx' }) +There are several ways to access a MongoDB database. You can make use of a cloud provider such as [mLab](https://mlab.com) or you can install the database locally on your development machine. -const sql = 'INSERT INTO books (id, title, authors, description) VALUES (NULL, "The Hobbit, "J.R.R. Tolkien", "Ring found")' +#### 2.1.1 Installing on Ubuntu -connection.query(sql, (err, rows) => { - if (err) callback( Error(`database error: ${err}`) ) - return callback(null, rows) -}) -``` -## Document Database -Stores JavaScript objects +#### 2.1.2 Installing on MacOS -Can be queried using JavaScript +The best way to install it on a Mac is to use the [Homebrew](https://brew.sh) package manager. The installation instructions can be found in the introductory chapter of this book. To install MongoDB +``` +brew install mongodb +``` +You need to create a data directory and change its permissions before starting the database server. +``` +$ mkdir -p /data/db +$ sudo chown -R `id -un` /data/db +$ mongod +``` Mongoose Example. @@ -109,6 +107,29 @@ if (err) { Good for modelling relationships +## Relational Database + +Familiar data storage + +Supports complex queries + +Difficult to scale + +Data needs normalising +```javascript +const mysql = require('mysql') +const request = require('request') + +const connection = mysql.createConnection({ host: 'xxx', port: '3306', user: 'xxx', password: 'xxx', database: 'xxx' }) + +const sql = 'INSERT INTO books (id, title, authors, description) VALUES (NULL, "The Hobbit, "J.R.R. Tolkien", "Ring found")' + +connection.query(sql, (err, rows) => { + if (err) callback( Error(`database error: ${err}`) ) + return callback(null, rows) +}) +``` + ## Choosing a Database Factors: