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: 1 addition & 0 deletions quiz copy/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('./src/server');
25 changes: 25 additions & 0 deletions quiz copy/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "cl_project",
"version": "1.0.0",
"description": "This is a sample node project",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"code",
"louisville",
"sample",
"project"
],
"author": "Aaron W. Johnson",
"license": "ISC",
"dependencies": {
"angular": "^1.6.1",
"body-parser": "^1.16.0",
"express": "^4.14.0",
"jquery": "^3.1.1",
"mongoose": "^4.8.4",
"pug": "^2.0.0-beta10"
}
}
80 changes: 80 additions & 0 deletions quiz copy/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# FSJS Project Week 1

## Install NodeJS

- [Windows (http://blog.teamtreehouse.com/install-node-js-npm-windows)](http://blog.teamtreehouse.com/install-node-js-npm-windows)
- [Mac (http://blog.teamtreehouse.com/install-node-js-npm-mac)](http://blog.teamtreehouse.com/install-node-js-npm-mac)
- [Linux (http://blog.teamtreehouse.com/install-node-js-npm-linux)](http://blog.teamtreehouse.com/install-node-js-npm-linux)

## Start a project
Starting a project in node is simple:
```
mkdir my_awesome_project
cd my_awesome_project
npm init
```

`npm init` simply creates a `package.json` file a populates it with the answers to some questions. You can edit it in a text editor.

## Sample project organization
When starting a project, a good practice is to lay out your directory structure and create some empty, basic files:
```
.
├── index.js // Entry point
├── package.json
└── src
├── config // application configuration
│   └── index.js
├── models // Database models
│   └── index.js
├── routes // HTTP(S) routing/controllers
│   └── index.js
└── server.js // Set up server and listen on port
```

## Add a library
Perhaps the primary use of `npm` is to add packages to your project. We're going to add the 'E' of 'MEAN' to our project right now:

```
npm install express --save
```

This tells `npm` to download the 'express' package, save it in a newly created `node_modules` directory, and then add a line in `package.json` to make note of the fact that we need 'express' for this project (that's what the `--save` part does).

## require() is a big deal
Yes it is. The full documentation for require() (really, for Node modules in general) can be found [here (https://nodejs.org/api/modules.html)](https://nodejs.org/api/modules.html).

`require()` is what allows you to organize your code in to easy-to-understand (hopefully) directories and files, but join them all together in to a single application.

For comparison: in a browser environment, if you want to make content from multiple file available to the larger application you can 1) concatenate them all in to one file or 2) load them individually via a `<script>` tag. Then, the objects or functions in the file need to be made available by putting them in the global scope (which is `window` in a browser) or be added to some global object (like, say, `jQuery` via a plugin);

`require()` serves that purpose on the server side by reading the contents of the file you specify, executing it, and making whatever you export available.
```javascript
// index.js
var myRandomObject = require('./myFile');

// myFile.js
exports = {some: {random: ['object']}};
```

### What's with all these index.js files
You will see (and create) a lot of `index.js` files in your Node lifetime. The reason for this has to do with how `require()` behaves.

When you pass the name of a directory to `require()`, it will specifically seek out a file in that directory named `index.js` (if it doesn't find one, it looks for index.node, but that's a story for another time)


### So this can be confusing.
Your text editor may have half a dozen open tabs - all with the name `index.js`. That's annoying, but the `index.js` naming convention is there for good reason and it is an important aspect of nodejs development.

Remember, you don't HAVE to have an `index.js` file in a directory, but you should know how Node treats that file if you do.

A couple of suggestions:
- Learn to pay attention to the names of directories as much as you pay attention to the names of files
- Add a comment at the top of the file that tells you where you are. For example:
```javascript
// some/directory/index.js
```
or
```javascript
// yet/another/directory/index.js
```
1 change: 1 addition & 0 deletions quiz/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('./src/server');
25 changes: 25 additions & 0 deletions quiz/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "cl_project",
"version": "1.0.0",
"description": "This is a sample node project",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"code",
"louisville",
"sample",
"project"
],
"author": "Aaron W. Johnson",
"license": "ISC",
"dependencies": {
"angular": "^1.6.1",
"body-parser": "^1.16.0",
"express": "^4.14.0",
"jquery": "^3.1.1",
"mongoose": "^4.8.4",
"pug": "^2.0.0-beta10"
}
}
98 changes: 98 additions & 0 deletions quiz/public/css/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
h1 {
color: green;
}

li {
cursor: pointer;
}
li.selected {
background-color: pink
}

*{
box-sizing: border-box;
}

body, html{
font-family: 'Open Sans', sans-serif;
background: #fff;
padding: 0;
margin: 0;
font-size: 16px;
}

h1{
color: red;
}

.wrapper{
background: #fff;
max-width: 700px;
margin: 0 auto;
padding: 30px;
}

ul{
margin: 0 0 40px 0; padding: 0; overflow: auto;
}

li, label{
list-style-type: none;
padding: 20px;
width: 48%;
margin: 1%;
float: left;
text-align: center;
background-color: #efefef;
border: 1px solid #ccc;
min-height: 85px;
}

label{
-webkit-box-flex: 1;
-moz-box-flex: 1;
-webkit-flex: 1;
-ms-flex: 1;
flex: 1;
}

.row {
display: -webkit-box;
display: -moz-box;
display: -webkit-flexbox;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
}

li:nth-child(odd), label:nth-child(odd){
margin-left:0%;
}

label:hover{
cursor: pointer;
}

input:checked + label {
background: pink;
}

input[type=radio]{
display: none;
}

button, input[type=submit]{
background: red;
border: 0px;
padding: 20px;
color: #fff;
margin: 10px 0;
font-size: 20px;
text-transform: uppercase;
font-family: 'Open Sans', sans-serif;
}

.question{
overflow: auto;
margin-bottom: 40px;
}
58 changes: 58 additions & 0 deletions quiz/public/js/component/file-list.component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
angular.module("fileList").component("fileList",{
templateUrl: "/js/template/file-list.template.html",
controller: function FileListController($http){
var self = this;
/*
$http.get("/files").then(function(response){
self.files=response.data;
});
*/

this.getFiles = () => {
return $http.get("/files").then(function(response){
return self.files=response.data;
});
}
this.getFiles();

this.selectFile = (file) => {
// get the currently selected file and set the `selected`
// property to false
const currentlySelected = this.selectedFile;
this.selectedFile = null;

// Mark the passed file as selected
if (!currentlySelected || currentlySelected._id !== file._id) {
this.selectedFile = angular.copy(file);
}
}


//update file
this.updateFile = (file) => {

$http.put(`/files/${file._id}`, {file})
.then(response => {
console.log("Successfully updated file");
return this.getFiles();
})
.catch(err => {
console.log("Oops...there was an error", err);
})
}


// Delete a file
this.deleteFile = (file) => {
$http.delete(`/files/${file._id}`)
.then(response => {
console.log("Successfully deleted file");
this.selectedFile = null;
return this.getFiles();
})
.catch(err => {
console.log("Drat...there was an error", err);
})
}
}
});
1 change: 1 addition & 0 deletions quiz/public/js/module/app.module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
angular.module("fileListApp",["fileList"]);
1 change: 1 addition & 0 deletions quiz/public/js/module/file-list.module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
angular.module("fileList",[]);
38 changes: 38 additions & 0 deletions quiz/public/js/template/file-list.template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

<form action ="">
<div class="question" ng-repeat="file in $ctrl.files">
<h2>{{file.qname}}</h2>
<div class="row">
<input type="radio" name="q{{$index + 1}}" value="1" id="q1{{$index + 1}}"> <label for="q1{{$index + 1}}">{{file.choose1}}</label>
<input type="radio" name="q{{$index + 1}}" value="2" id="q2{{$index + 1}}"> <label for="q2{{$index + 1}}">{{file.choose2}}</label>
</div>
<div class="row">
<input type="radio" name="q{{$index + 1}}" value="3" id="q3{{$index + 1}}"> <label for="q3{{$index + 1}}">{{file.choose3}}</label>
<input type="radio" name="q{{$index + 1}}" value="4" id="q4{{$index + 1}}"> <label for="q4{{$index + 1}}">{{file.choose4}}</label>
</div>
</div>
<input type="Submit" value="Get Results">
</form>
<!-- public/js/template/file-list.template.html -->



<!--
<div ng-show="$ctrl.selectedFile">
<h4>Editing: {{$ctrl.selectedFile.title}}</h4>
<fieldset>
<div>
<label>Title: <input ng-model="$ctrl.selectedFile.title" /></label>
</div>
<div>
<label>Filename: <input ng-model="$ctrl.selectedFile.filename" /></label>
</div>
<div>
<button ng-click="$ctrl.updateFile($ctrl.selectedFile)">Update File</button>
<button ng-click="$ctrl.deleteFile($ctrl.selectedFile)">Delete</button>
</div>
</fieldset>
</div>
-->


Binary file added quiz/public/puppy.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading