Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Node_Week_2/expressworks/Part 2/9.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,3 @@ app.get('/json', function(req, res) {
}
})
```

5 changes: 3 additions & 2 deletions Node_Week_2/expressworks/concepts/Delete Requests.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<!--title={A Closer Look at Delete}-->

The DELETE method will delete some data or file from the server side.
The DELETE method will delete data from the server. It is the **D** in **CRUD**. If we want to delete some data on the file route, we will use ``/file`` as the handle and delete it like so:

```javascript
app.delete ('/file', function (req, res) { // DELETE request to the 'file' route
app.delete ('/file:id', function (req, res) {
const movie = req.params.id;
res.send('deleted it!')
})
```
Expand Down
41 changes: 32 additions & 9 deletions Node_Week_2/expressworks/concepts/GET Requests.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,44 @@
<!--title={A Closer Look at GET}-->

GET requests fetch data from specified resource.
GET requests retrieve data from server. It is the **Read** of the **R** in **CRUD**.

Lets say we want to retrieve some data from a server every time someone visits the homepage of our website. We would use a **get** request with ``/home`` as the handle:

```javascript
app.get('handle', function (req, res) {
//code to perform a specified action
app.get('/home', function (req, res) {
//code to perform a specified action
})
```

In this case, GET is fetching the the data from the handle.
Lets say that on the home page, we have some information that we would like to retreive. In the home.html file, it contains:

```html
<html>
<body>
<form action="/home" method="GET">
Favorite Movie: <input type="text" movie="movie_name"/> <br/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
```

Often, the handle is simply `/` which is the root:
Now, when we use the **get** request, we can use data from the server to perform an action:

```javascript
app.get('/', function (req, res) {
//code to perform a specified action
res.send('got it!')
})
app.get('/home', function (req, res) {
res.send('<p>Favorite Movie: ' + req.query['movie']'</p>');
})
```

Lets say instead you want to get all of your favorite movies of a page called ``fav_movies``:

```js
app.get('/fav_movie:movies', function (req, res) {
//TODO: add in another use-case
})


```

![An example GET request on Github](https://res.cloudinary.com/indysigner/image/fetch/f_auto,q_auto/w_400/https://cloud.netlifyusercontent.com/assets/344dbf88-fdf9-42bb-adb4-46f01eedd629/b6d1a8b7-51ef-45d2-a416-b34d7c76abda/understanding-api-doc-github-repo-get-opt.png)
10 changes: 5 additions & 5 deletions Node_Week_2/expressworks/concepts/POST Requests.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!--title={A Closer Look at POST}-->

The POST method can be used to send large amounts of information to a specified resource.
POST creates data in server and is the **C** in **CRUD**.

To use post, first install **bodyParser** in the package file:

Expand All @@ -19,12 +19,12 @@ app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
```

Now, we can use post like so:
Now, we can use post! Lets say we want to create some data on a server every time someone visits the homepage of our website. We would use a **post** request with ``/home`` as the handle:

```js
app.post('/', function (req, res) { // The '/'is the handler to the root route
//code to perform any specified action
res.send('posted!')
app.post('/home:id', function (req, res) {
const movie = req.params.id;
res.send('posted!')
})
```

Expand Down
7 changes: 4 additions & 3 deletions Node_Week_2/expressworks/concepts/PUT Requests.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<!--title={A Closer Look at PUT}-->

PUT also sends data to a resource. Unlike POST, if the data already exists there (with the same file name), PUT replaces that file. If there is no file there, PUT will create one.
PUT updates and replaces data in server. It is the **U** in **CRUD**. Unlike POST, if the data already exists there (with the same file name), PUT replaces that file. If there is no file there, PUT will create one.

```javascript
app.put('/user', function (req, res) { // PUT request to the 'user' route
res.send('put it!')
app.put('/user/:id', function (req, res) {
const movie = req.params.id;
res.send('put it!')
})
```

140 changes: 95 additions & 45 deletions Node_Week_2/js-best-practices/Part 1/1.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,52 +17,102 @@ as some bad practices to avoid. Here's an overview of the concepts we're going t
Below is the code for a file you should name `vendingMachine.js`. Although the code works, you'll fix the code so that the code is much more maintanable and understandable.

```js
var fs = require('fs');
var path = require('path');
var shop = require('../../js-best-practices');

module.exports = function buildExercise(){
printProblem().then(function(){
return waitForInput('press any key to continue...');
}).then(function(){
shop.execute(['init']);
});
var balanceManager = require('./balanceManager');
var changeHandler = require('./changeHandler');
var productInventory = require('./productInventory');

var balance = 0;

var products = [
{
name: 'Skittles',
price: 85,
id: 'A1'
}
];

module.exports = {
canAfford: function(amount){
if(!this.isValidAmount(amount)){
errorMessage = "Invalid Input";
}
if(errorMessage){
throw new Error(errorMessage);
}
return amount <= balance;
},

decreaseBalance: function(amount){
// This method decreases the balance of the vending machine. If the balance amount is not
// enough to cover the purchase, the method throws an error.
var errorMessage;
if(!this.canAfford(amount)){
errorMessage = 'Insufficient balance';
}
if(errorMessage){
throw new Error(errorMessage);
}
balance -= amount;
},

getAmount: function(coinType) {
// COINS:
// [p]enny
// [n]ickel
// [d]ime
// [q]uarter
switch(coinType){
case 'p': return 1;
case 'n': return 5;
case 'd': return 10;
case 'q': return 25;
default: throw new Error('Unrecognized coin ' + coinType);
}
},

getBalance: function(){
return balance;
},

getProducts: function() {
return products;
},

getProduct: function(productId) {
var product = products.find(function(p) { return p.id === productId; });
return product;
},

return {
noprint: true
};
}

function waitForInput(prompt){
return new Promise(function(res, rej){
console.log(prompt);
var alreadyRaw = process.stdin.isRaw;
process.stdin.setRawMode(true);
process.stdin.resume();
process.stdin.once('data', function(){
process.stdin.pause();
process.stdin.setRawMode(alreadyRaw);
res();
});
});
}

function printProblem(){
return new Promise(function(res, rej){
var lang = shop.i18n.lang()
var problem = fs.readFileSync(path.join(__dirname, 'problem' + (lang === 'en' ? '' : '.' + lang) + '.md'));
var stream = shop.createMarkdownStream({
meta: {
name: 'get started',
number: 0
}
});
stream.append(problem, 'md');
stream.pipe(require('workshopper-adventure/lib/mseePipe')())
.pipe(process.stdout)
stream.on('end', res);
});
}
increaseBalance: function(amount){
balance += amount;
},

insertCoin: function(coinType){
var value = this.getAmount(coinType);
this.increaseBalance(value);
},

isValidAmount: function(amount){
if(amount === null){
return false;
} else {
return true;
}
},

releaseChange: function(){
var currentBalance = this.getBalance();
this.decreaseBalance(currentBalance);
return this.convertToChange(currentBalance);
},

vendProduct: function(productId){
var product = this.getProduct(productId);
this.decreaseBalance(product.price);
return product;
}

};
```


Expand Down
2 changes: 1 addition & 1 deletion Node_Week_2/js-best-practices/Part 1/2.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!--title={Separating files}-->


Now that we've

# Separation of Concerns Part 1

Expand Down