diff --git a/.gitignore b/.gitignore index cd2946ad..26957f4d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ +#project local +node_modules + + # Windows image file caches Thumbs.db ehthumbs.db diff --git a/Each lesson Codes b/Each lesson Codes deleted file mode 100644 index 8b137891..00000000 --- a/Each lesson Codes +++ /dev/null @@ -1 +0,0 @@ - diff --git a/Practice/#1 code of the play list.js b/Practice/#1 code of the play list.js deleted file mode 100644 index 1eea90c9..00000000 --- a/Practice/#1 code of the play list.js +++ /dev/null @@ -1,6 +0,0 @@ -console.log("Hello Saif, You are learning Node.js!"); - -setTimeout(function(){ - console.log("Three seconds \n have passed!"); - -},3000); \ No newline at end of file diff --git a/Practice/#10 File Read and Write/app.js b/Practice/#10 File Read and Write/app.js deleted file mode 100644 index 78d0631d..00000000 --- a/Practice/#10 File Read and Write/app.js +++ /dev/null @@ -1,11 +0,0 @@ -//Generally the module name and the variable name both are same -var fs = require('fs'); - -// fs.readFileSync -// Sync' part allows the node to read the file synchronusly meaning all file is read first before going through other code. -var sample = fs.readFileSync('sample.txt','utf8'); -// utf8 is encoding format| you can find clean explanation here at http://stackoverflow.com/a/15128103/5388823 -console.log(sample); - -// this line of code creates an another file output.txt and writes the data in sample into the log. -fs.writeFileSync('output.txt',sample); diff --git a/Practice/#10 File Read and Write/sample.txt b/Practice/#10 File Read and Write/sample.txt deleted file mode 100644 index 6f813825..00000000 --- a/Practice/#10 File Read and Write/sample.txt +++ /dev/null @@ -1 +0,0 @@ -Here goes the content of your file. \ No newline at end of file diff --git a/Practice/#11 Asyc/app.js b/Practice/#11 Asyc/app.js deleted file mode 100644 index ac8b3621..00000000 --- a/Practice/#11 Asyc/app.js +++ /dev/null @@ -1,8 +0,0 @@ -var fs = require('fs'); - -var file = fs.readFile('input.txt','utf8',function(err,data){ - fs.writeFile('writeme.txt',data); - console.log(data); -}); - -console.log('This is an instruction outside the sync file system.'); \ No newline at end of file diff --git a/Practice/#11 Asyc/input.txt b/Practice/#11 Asyc/input.txt deleted file mode 100644 index fc8c1f43..00000000 --- a/Practice/#11 Asyc/input.txt +++ /dev/null @@ -1 +0,0 @@ -This is file to read Synchonusly. \ No newline at end of file diff --git a/Practice/#2 callfunction.js b/Practice/#2 callfunction.js deleted file mode 100644 index b455f345..00000000 --- a/Practice/#2 callfunction.js +++ /dev/null @@ -1,9 +0,0 @@ -function callFunction(fun){ - fun(); -} - -var tata = function(){ - console.log('bye'); -} - -callFunction(tata); \ No newline at end of file diff --git a/Practice/#3 dirname.js b/Practice/#3 dirname.js deleted file mode 100644 index c2d2e35d..00000000 --- a/Practice/#3 dirname.js +++ /dev/null @@ -1,4 +0,0 @@ -// node can also determine the your directory by using '__dirname'; - -console.log(__dirname); -console.log(__filename); diff --git a/Practice/#4 event.js b/Practice/#4 event.js deleted file mode 100644 index 5abf0d5b..00000000 --- a/Practice/#4 event.js +++ /dev/null @@ -1,9 +0,0 @@ -var events = require('events'); - -var myEmmitter = new events.EventEmitter(); - -myEmmitter.on('anEvent',function(msg){ - console.log(msg); -}); - -myEmmitter.emit('anEvent','The event is absolutely emmited'); \ No newline at end of file diff --git a/Practice/#5 getmodule.js b/Practice/#5 getmodule.js deleted file mode 100644 index db6826e3..00000000 --- a/Practice/#5 getmodule.js +++ /dev/null @@ -1,11 +0,0 @@ -// This is the module getting function. it gets its module from moduleexp.js - -var stuff = require('./moduleexp.js');// | ./ | is used to search in present directory. - - -// look here at the counter part, it is actually taking the arguments. and doing everything. -// We need to intialise the 'counter variable first.' -console.log(stuff.counter(['Hello','This is','Saif'])); -console.log(stuff.adder(5,6)); -console.log(stuff.bi); -console.log(stuff.adder(stuff.bi,44)); \ No newline at end of file diff --git a/Practice/#6 moduleexp.js b/Practice/#6 moduleexp.js deleted file mode 100644 index 47b53339..00000000 --- a/Practice/#6 moduleexp.js +++ /dev/null @@ -1,19 +0,0 @@ - -// This file is completely a module for getmodule.js file. - - -// a function expression is made here. -var counter = function(arry){ - return 'The number of '+arry.length + '\n done!'; -}; -// Adding more modules -var adder = function(a,b){ - return 'On adding the two number it gives'+(a+b); -} - -var pi = 3.1535; - -// module.exports is the important part, it makes the counter available for other modules! -module.exports.counter = counter ; -module.exports.adder = adder; -module.exports.bi = pi; \ No newline at end of file diff --git a/Practice/#7 num 3.js b/Practice/#7 num 3.js deleted file mode 100644 index b92b8d45..00000000 --- a/Practice/#7 num 3.js +++ /dev/null @@ -1,14 +0,0 @@ - -// General Method for writing a function! -function write(){ - console.log('Woah! I just Invoked a function'); -} -write(); - -// Writing a funcion expression - -var sayBye = function(){ - console.log('I just called a funcion expression,!'); -} - -sayBye(); \ No newline at end of file diff --git a/Practice/#8 setInterval.js b/Practice/#8 setInterval.js deleted file mode 100644 index 0a99b219..00000000 --- a/Practice/#8 setInterval.js +++ /dev/null @@ -1,8 +0,0 @@ -var time = 0; -var timer = setInterval(function(){ - console.log(time+ ' seconds have passed'); - time += 2; - if(time > 8){ - clearInterval(timer); - } -},2000); \ No newline at end of file diff --git a/Practice/#9 util.js b/Practice/#9 util.js deleted file mode 100644 index 9a01fd22..00000000 --- a/Practice/#9 util.js +++ /dev/null @@ -1,29 +0,0 @@ -// Adding the required Modules - -var events = require('events'); -var util = require('util'); -// creating a function Person - -var Person = function(name){ -// this allows, the function with any object created(james,saif,sampath) to utilise this function - this.name = name; -} - -// From the module util, we are inheriting the from events.EventEmitter -util.inherits(Person,events.EventEmitter); -//randomly 3 objects storing in 3 variables -var james = new Person('james'); -var saif = new Person('saif'); -var sampath = new Person('sampath'); - -var People = [james,saif,sampath]; - -People.forEach(function(Person){ - Person.on('speak',function(msg){ - console.log(Person.name + 'said this ' + msg ); - }); -}); - -james.emit('speak','This is james'); -saif.emit('speak','OH great nice to meet you Mer. '); -sampath.emit('speak','Thats cool'); \ No newline at end of file diff --git a/README.md b/README.md index be052def..f576b8ed 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/app.js b/app.js new file mode 100644 index 00000000..5d0b49b3 --- /dev/null +++ b/app.js @@ -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'); diff --git a/controllers/todoController.js b/controllers/todoController.js new file mode 100644 index 00000000..18bfd617 --- /dev/null +++ b/controllers/todoController.js @@ -0,0 +1,49 @@ +var bodyParser = require('body-parser'); +var mongoose = require('mongoose'); + + +var uri="mongodb://test:test@ds129023.mlab.com: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); + }); + }); + + + + +}; diff --git a/package.json b/package.json new file mode 100644 index 00000000..8f293527 --- /dev/null +++ b/package.json @@ -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" + } +} diff --git a/public/assets/styles.css b/public/assets/styles.css index 4cbb3825..ee7623d8 100644 --- a/public/assets/styles.css +++ b/public/assets/styles.css @@ -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; } diff --git a/public/assets/todo-list.js b/public/assets/todo-list.js index 0afe998b..da7c82dd 100644 --- a/public/assets/todo-list.js +++ b/public/assets/todo-list.js @@ -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, diff --git a/views/todo.ejs b/views/todo.ejs new file mode 100644 index 00000000..d64b7f03 --- /dev/null +++ b/views/todo.ejs @@ -0,0 +1,25 @@ + + +
+