Skip to content

Commit

Permalink
more updates and script.js into xhr
Browse files Browse the repository at this point in the history
  • Loading branch information
shannonjensen committed Mar 19, 2018
1 parent 619bd5e commit 30c2158
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 43 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ We believe in learning by doing!

### Structure of the workshop

You will be working on this project in pairs. Your mentors will be available to help you out and answer any questions. Remember the 20/20/20 rule if you get stuck!
You will be working on this project in pairs. Your mentors will be available to help you out and answer any questions. Remember the 20/20/20 rule if you get stuck.

**First, clone this repository onto your own machine**

Expand Down
2 changes: 1 addition & 1 deletion public/index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!DOCTYPE html>
<html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Node Girls CMS tutorial</title>
Expand Down
54 changes: 29 additions & 25 deletions public/script.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
$(document).ready(function() {
$.ajax({
url: '/posts',
dataType: 'json',
success: function(data) {

document.onreadystatechange = function () {
if (document.readyState === 'complete') {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if(xhr.readyState === 4){
if (xhr.status === 200){
var data = JSON.parse(xhr.responseText);
for (var blogPost in data) {
var postDiv = document.createElement('div');
var postText = document.createElement('p');
var thumbnail = document.createElement('img');
var postContainer = document.getElementsByClassName('post-container')[0];

thumbnail.src = "./img/logo2.png";
thumbnail.className = "thumbnail";
postText.innerHTML = data[blogPost];
postDiv.className = "post";

postDiv.appendChild(thumbnail);
postDiv.appendChild(postText);
postContainer.appendChild(postDiv);

var postDiv = document.createElement('div');
var postText = document.createElement('p');
var thumbnail = document.createElement('img');
var postContainer = document.getElementsByClassName('post-container')[0];

thumbnail.src = "./img/logo2.png";
thumbnail.className = "thumbnail";
postText.innerHTML = data[blogPost];
postDiv.className = "post";

postDiv.appendChild(thumbnail);
postDiv.appendChild(postText);
postContainer.appendChild(postDiv);
}
},
error: function(error){
console.log(error);
}
else {
console.error(xhr.responseText);
}
}
});
});
}
xhr.open('GET', '/posts', true);
xhr.send();
}
}
5 changes: 4 additions & 1 deletion step04.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@ Let's try it.

**Require `fs` module at the top of your file**

Edit your code to include the following route:

```js
var http = require('http');
var fs = require('fs');

function router (request, response) {

//...
if (endpoint === "/") {
response.writeHead(200, {"Content-Type": "text/html"});

Expand All @@ -75,6 +77,7 @@ function router (request, response) {
response.end(file);
});
}
//...
}
```

Expand Down
21 changes: 16 additions & 5 deletions step07.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ we do this:
var myModule = require('path/to/my/file');
```

**About Paths**

We can use **relative paths**. `..` means "go up one level to the folder above". `.` means "look inside the current folder". For example: '../myComputer/home.txt' means "go up one folder, then go to the 'myComputer' folder, then go to the 'home.txt' file". Another example: './home.txt' means "look inside the current folder for the 'home.txt' file"

Remember that `__dirname` gives us the current directory. We can combine `__dirname`, the dots, and any folder names to point to the correct location.

**Require your router.js file into your server.js file**

If you run your server, you should get an error:
Expand All @@ -64,15 +70,20 @@ Try running the server again and reloading the page.

## Fixing your broken paths

You should see a very broken webpage now. There's a reason for that!
You webpage is now very broken. There's a reason for that!

When the router function was in `server.js`, the paths to our assets (html, css etc) that were assigned to the `pathToFile` and `pathToIndex` variables, were correct. Now the router function has moved inside the `src` folder, we need to update the paths to our assets to make sure they point to the right location.

We can use **relative paths**. `..` means "go up one level to the folder above". `.` means "look inside the current folder". For example: '../myComputer/home.txt' means "go up one folder, then go to the 'myComputer' folder, then go to the 'home.txt' file". Another example: './home.txt' means "look inside the current folder for the 'home.txt' file"

Remember that `__dirname` gives us the current directory. We can combine `__dirname`, the dots, and any folder names to point to the correct location.
Refer to the About Paths section above for guidance and don't be afraid to ask questions.

See if you can do this by yourself. Work with your team if you're struggling.
Hint that it should look like:
```js
__dirname + '/../public/index.html'
```
or
```js
path.join(__dirname, '..', 'public', 'index.html')
```


## More modularisation!!!
Expand Down
3 changes: 2 additions & 1 deletion step08.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

For the rest of the workshop your job is to build the proper CMS. We have done the front-end code for you - you only need to worry about the server side of the application.

Delete everything from your router function except the handlers for the home route "/" and the generic assets.

We need to change the `index.html` file. Replace the current content with:

```html
Expand Down Expand Up @@ -36,7 +38,6 @@ We need to change the `index.html` file. Replace the current content with:

<div class="post-container"></div>
</main>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
</body>
</html>
```
Expand Down
10 changes: 1 addition & 9 deletions step09.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,7 @@

Run the server to see what the CMS looks like!


*Everything is broken...*

Fix it!

Your job is to write a new server and handler. You'll serve the public assets and handle any embedded requests in the `index.html`.


Good luck! Remember, work with your team and chat with your mentor!
Make sure that all paths are working and all assets are being properly served.

---

Expand Down

0 comments on commit 30c2158

Please sign in to comment.