Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete todo app #7

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#project local
node_modules


# Windows image file caches
Thumbs.db
ehthumbs.db
Expand Down
1 change: 0 additions & 1 deletion Each lesson Codes

This file was deleted.

6 changes: 0 additions & 6 deletions Practice/#1 code of the play list.js

This file was deleted.

11 changes: 0 additions & 11 deletions Practice/#10 File Read and Write/app.js

This file was deleted.

1 change: 0 additions & 1 deletion Practice/#10 File Read and Write/sample.txt

This file was deleted.

8 changes: 0 additions & 8 deletions Practice/#11 Asyc/app.js

This file was deleted.

1 change: 0 additions & 1 deletion Practice/#11 Asyc/input.txt

This file was deleted.

9 changes: 0 additions & 9 deletions Practice/#2 callfunction.js

This file was deleted.

4 changes: 0 additions & 4 deletions Practice/#3 dirname.js

This file was deleted.

9 changes: 0 additions & 9 deletions Practice/#4 event.js

This file was deleted.

11 changes: 0 additions & 11 deletions Practice/#5 getmodule.js

This file was deleted.

19 changes: 0 additions & 19 deletions Practice/#6 moduleexp.js

This file was deleted.

14 changes: 0 additions & 14 deletions Practice/#7 num 3.js

This file was deleted.

8 changes: 0 additions & 8 deletions Practice/#8 setInterval.js

This file was deleted.

29 changes: 0 additions & 29 deletions Practice/#9 util.js

This file was deleted.

10 changes: 2 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
# node-js-playlist
CSS and asset files for the Net Ninja YouTube nodejs playlist
# Todo app
This is a web app created in Node.js, using mongodb

The final project code can be found in the public/assests folder of this repo

If you have been following the tutorial, code for each and every lesson is added in the Practice folder so you can directly download and check.
All files have been tested.

If more files for .\Practice\ should be added. They will be added very soon.
18 changes: 18 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var express = require('express');
var todoController = require('./controllers/todoController');


var app = express();

// Set up template engine
app.set('view engine', 'ejs');

// static files
app.use(express.static('./public'));

// fire controllers
todoController(app);

// listen to port
app.listen('3000');
console.log('You are listening to port 3000');
49 changes: 49 additions & 0 deletions controllers/todoController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
var bodyParser = require('body-parser');
var mongoose = require('mongoose');


var uri="mongodb://test:[email protected]:29023/todo-db-vpn";

//Connect to database
mongoose.connect(uri);

// Create a schema - this is like a blueprint
var todoSchema = new mongoose.Schema({
item: String
});

var Todo = mongoose.model('Todo', todoSchema);

// var data = [{item: 'get milk'}, {item: 'Walk dog'}, {item: 'Do some code'}];
var urlencodedParser = bodyParser.urlencoded({extended: false});

module.exports = function(app) {

app.get('/todo', function(req, res){
// get data from the mongodb and pass it to view
Todo.find({}, function(err, data){
if(err) throw err;
res.render('todo', {todos: data});
});
});

app.post('/todo', urlencodedParser, function(req, res){
// get data from the view and add it to mongodb
var newTodo = Todo(req.body).save(function(err, data){
if(err) throw err;
res.json(data);
});
});

app.delete('/todo/:item', function(req, res){
// delete the requested item from mongodb
Todo.find({item: req.params.item.replace(/\-/g, " ")}).remove(function(err, data){
if (err) throw err;
res.json(data);
});
});




};
26 changes: 26 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "todo-app",
"version": "1.0.0",
"description": "A little app to use as a To Do list",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/imvpn22/node-js-playlist.git"
},
"author": "imvpn22, netninja",
"license": "ISC",
"bugs": {
"url": "https://github.com/imvpn22/node-js-playlist/issues"
},
"homepage": "https://github.com/imvpn22/node-js-playlist#readme",
"dependencies": {
"body-parser": "^1.17.2",
"ejs": "^2.5.6",
"express": "^4.15.3",
"mongodb": "^2.2.30",
"mongoose": "^4.11.4"
}
}
2 changes: 1 addition & 1 deletion public/assets/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ li:hover{
}

h1{
background: url(/logo.png) no-repeat center;
background: url(/assets/logo.png) no-repeat center;
margin-bottom: 0;
text-indent: -10000px;
}
2 changes: 1 addition & 1 deletion public/assets/todo-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ $(document).ready(function(){
});

$('li').on('click', function(){
var item = $(this).text().replace(/ /g, "-");
var item = $(this).text().replace(/ /g, '-');
$.ajax({
type: 'DELETE',
url: '/todo/' + item,
Expand Down
25 changes: 25 additions & 0 deletions views/todo.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html>
<head>
<title>Todo List</title>
<script src="http://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<link href="/assets/styles.css" rel="stylesheet" type="text/css">
<script src="/assets/todo-list.js"></script>
</head>
<body>
<h1>My todo list</h1>
<div id="todo-table">
<form>
<input type="text" name="item" placeholder="Add new item..." required />
<button type="submit">Add Item</button>
</form>

<ul>
<% for(var i=0; i < todos.length; i++) { %>
<li><%= todos[i].item %></li>
<% } %>
</ul>

</div>
</body>
</html>