Skip to content
Permalink
Browse files
added content to chapter 1
  • Loading branch information
Mark Tyers committed May 14, 2017
1 parent a1cb36a commit 40f08a9f7ec0cd439f276dc8769ee339524db86d
Show file tree
Hide file tree
Showing 5 changed files with 353 additions and 63 deletions.
@@ -44,7 +44,7 @@ nvm install 7.9.0
node -v
v7.9.0
```
We install the latest version and then set this as default. Finally, to check everything worked we check to see ehat version is installed.
We install the latest version and then set this as default. Finally, to check everything worked we check to see what version is installed.

## 2 Variables and Scope

This file was deleted.

@@ -1,32 +1,20 @@
'use strict'

/* the readline-sync module allows NodeJS to read input from the terminal. */
const readline = require('readline-sync')

/* Here we declare an empty array which will be used to store the todo list items. */
var items = []
const items = []

/* the do-while loop has its exit condition at the end which means it always executes at least once. */
do {
/* We capture the text input by the user. This is then converted into a String and finally any _whitespace_ (newline characters, spaces, etc) are removed. */
var input = String(readline.question('enter command: ')).trim() // try switching for a let... or a const...
/* The indexOf() function looks for the first ocurrance of the supplied string parameter or -1 if it is not found. Notice the use of === to compare the two values, this is a 'strict' comparison and should always be used instead of ==. It checks for equal values and equal type. */
var input = String(readline.question('enter command: ')).trim()
if (input.indexOf('add ') === 0) {
/* This time the indexOf() function is used to locate the first space character. */
let space = input.indexOf(' ')
/* the substring() function returns the string after the specified position. This will include the space character and so the result is trimmed of any whitespace. */
let item = input.substring(space).trim()
/* console.log() prints to the terminal. */
console.log('adding "'+item+'"')
/* All arrays have a built-in push() function which appends an item to their end. */
const space = input.indexOf(' ')
const item = input.substring(space).trim()
console.log(`adding "${item}"`)
items.push(item)
}
if (input.indexOf('list') === 0) {
/* Here we use a for...next loop to interate through all the array indexes. The let keyword defines a variable with _block_ scope (more on this later). */
for (let i=0; i< items.length; i++) {
/* Here we reference the array index. */
console.log(i+'. '+items[i])
console.log(`${i}. ${items[i]}`)
}
}
/* the code will loop unless the input was _exit_. Notice the use of !== in the comparison instead of the more typical !=. This is a strict negating comparison. This means 'not equal and not equal type' */
} while (input !== 'exit')
@@ -3,6 +3,8 @@

This book covers advanced programming concepts using multiple paradigms. It is assumed that you are already comfortable writing and testing large amounts of code.

It is based on a long-running module at Coventry University called Web API Development.

By the end of this book you will:

- Have learned cutting-edge JavaScript skills
@@ -21,3 +23,72 @@ So why learn about developing web APIs?
## Book Structure

The book is divided into three parts. In part (chapters 1-3) one we cover the principles behind the latest JavaScript (ECMA) language. In part two (chapters 4-6) we cover how to design RESTful APIs and how to build these. In the final part we cover a range of advanced topics to help you when you start writing more complex API.

## How to Use This Book

Each chapter is divided into two sections. Part 1 covers the core features which you will absolutely need to know whilst Part 2 covers the more advanced features that you will find useful.

The learning revolves around reading and understanding code then testing your knowledge. Rather than printing out these scripts in their entirety, the book will highlight key concepts and give you a reference to the full code which is available on GitHub.

If you are an experienced programmer feel free to skim over the part 1 content and focus on the advanced stuff in part 2.

If you do not have lots of experience stick with the part 1 content then come back later, when you have more experience and tackle the content in part 2.

### Cloning the Sample Code

Whilst you could simply download the sample code from GitHub it is recommended that you clone this. The benefits are that as bugs are found and the exercise files improved, you can pull these changes into your local copy of the code.

Assuming you are running a *NIX system you can clone the materials. This command will create a `nodejs/` directory in your `Documents/` directory containing all the sample code.
```
git clone https://github.com/covcom/304CEM.git ~/Documents/nodejs
```

## Your System

All the examples and exercises in this book are based on an Ubuntu-based system however if you are not using this platform you have several choices:

1. You can take the plunge and install Ubuntu (either on its own or dual-book). There are plenty of online tutorials that cover this.
2. You can run Ubuntu as a virtual machine. One option is to use [VirtualBox](https://www.virtualbox.org) which is a free download and available for all major platforms.
3. Use a different flavour of Linux or Unix such as Fedora or MacOS (most of the commands will work just fine).
4. Stick with Windows and work around the issues.

## Installing NodeJS

Let's get started by installing the latest version of NodeJS. Whilst you would need to download and install a separate install for each version from the [NodeJS downloads page](https://nodejs.org/en/download/current/), because we are running a Linux distribution (you did read the previous section didn't you?) we can install the [Node Version Manager](https://github.com/creationix/nvm) and use this to install whatever version of Node we want. Lets fire up the Terminal. We need to use the cURL command to download the installation shell script and pipe it to the `bash` command to run it.
```
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash
```
The installation script has added a new path to your shell configuration and this needs to be loaded again. You have two choices, either log out then log in again or reload the shell using `source ~/.bashrc`.

Now we can use this tool to check for the versions of NodeJS available. The latest version (7.10.0 at the time of writing) will be at the end of the list.
```
nvm list-remote
...
v7.0.0
v7.1.0
v7.2.0
...
v7.10.0
```
Finally we can install the current (latest) version (v7.10.0) at the time of writing.
```
nvm install 7.10.0
Downloading https://nodejs.org/dist/v7.10.0/node-v7.2.0-linux-x64.tar.xz...
######################################################################## 100.0%
Now using node v7.10.0 (npm v3.10.9)
default -> 7.10.0 (-> v7.10.0)
```
Finally we can check which version is installed.
```
node -v
v7.10.0
```

## 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.

Note that you may need to update the configuration database before Visual Studio Code will install.
```
sudo apt-get update && sudo apt-get install libgconf2-4
```

0 comments on commit 40f08a9

Please sign in to comment.