From e11bb0c6f1cbf3e0e7652b5b4fc28e2460790e4a Mon Sep 17 00:00:00 2001 From: imvpn22 Date: Thu, 27 Jul 2017 20:38:08 +0530 Subject: [PATCH 1/6] Start --- .gitignore | 4 +++ Each lesson Codes | 1 - Practice/#1 code of the play list.js | 6 ----- Practice/#10 File Read and Write/app.js | 11 -------- Practice/#10 File Read and Write/sample.txt | 1 - Practice/#11 Asyc/app.js | 8 ------ Practice/#11 Asyc/input.txt | 1 - Practice/#2 callfunction.js | 9 ------- Practice/#3 dirname.js | 4 --- Practice/#4 event.js | 9 ------- Practice/#5 getmodule.js | 11 -------- Practice/#6 moduleexp.js | 19 -------------- Practice/#7 num 3.js | 14 ---------- Practice/#8 setInterval.js | 8 ------ Practice/#9 util.js | 29 --------------------- README.md | 9 ------- app.js | 28 ++++++++++++++++++++ package.json | 25 ++++++++++++++++++ 18 files changed, 57 insertions(+), 140 deletions(-) delete mode 100644 Each lesson Codes delete mode 100644 Practice/#1 code of the play list.js delete mode 100644 Practice/#10 File Read and Write/app.js delete mode 100644 Practice/#10 File Read and Write/sample.txt delete mode 100644 Practice/#11 Asyc/app.js delete mode 100644 Practice/#11 Asyc/input.txt delete mode 100644 Practice/#2 callfunction.js delete mode 100644 Practice/#3 dirname.js delete mode 100644 Practice/#4 event.js delete mode 100644 Practice/#5 getmodule.js delete mode 100644 Practice/#6 moduleexp.js delete mode 100644 Practice/#7 num 3.js delete mode 100644 Practice/#8 setInterval.js delete mode 100644 Practice/#9 util.js delete mode 100644 README.md create mode 100644 app.js create mode 100644 package.json 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 deleted file mode 100644 index be052def..00000000 --- a/README.md +++ /dev/null @@ -1,9 +0,0 @@ -# node-js-playlist -CSS and asset files for the Net Ninja YouTube nodejs playlist - -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..80bfd0fd --- /dev/null +++ b/app.js @@ -0,0 +1,28 @@ +var express = require('express'); +var bodyParser = require('body-parser'); +var app=express(); + +var urlencoderParser = bodyParser.urlencoded({extended: false}); + +app.set('view engine', 'ejs'); +app.use('/assets', express.static('stuff')); + +app.get('/', function(req, res){ + res.render('index'); +}); + +app.get('/contact', function(req, res){ + res.render('contact', {qs: req.query}); +}); + +app.post('/contact', urlencoderParser, function(req, res){ + console.log(req.body); + res.render('contact-success', {data: req.body}); +}); + +app.get('/profile/:name', function(req, res){ + var data = {age:22, job:'net ninja', hobies:['singing', 'dancing', 'fishing']}; + res.render('profile', {person: req.params.name, data: data}); +}); + +app.listen(3000); diff --git a/package.json b/package.json new file mode 100644 index 00000000..7f294cfd --- /dev/null +++ b/package.json @@ -0,0 +1,25 @@ +{ + "name": "node-js-playlist", + "version": "1.0.0", + "description": "A simple to do app", + "main": "app.js", + "dependencies": { + "body-parser": "^1.17.2", + "ejs": "^2.5.6", + "express": "^4.15.3" + }, + "devDependencies": {}, + "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" +} From cf95115b31295634367c2e42780e348f81766534 Mon Sep 17 00:00:00 2001 From: imvpn22 Date: Thu, 27 Jul 2017 21:07:46 +0530 Subject: [PATCH 2/6] 2nd commit --- app.js | 32 +++++++++++--------------------- controllers/todoController.js | 15 +++++++++++++++ package.json | 17 ++++++++--------- 3 files changed, 34 insertions(+), 30 deletions(-) create mode 100644 controllers/todoController.js diff --git a/app.js b/app.js index 80bfd0fd..5d0b49b3 100644 --- a/app.js +++ b/app.js @@ -1,28 +1,18 @@ var express = require('express'); -var bodyParser = require('body-parser'); -var app=express(); +var todoController = require('./controllers/todoController'); -var urlencoderParser = bodyParser.urlencoded({extended: false}); -app.set('view engine', 'ejs'); -app.use('/assets', express.static('stuff')); - -app.get('/', function(req, res){ - res.render('index'); -}); +var app = express(); -app.get('/contact', function(req, res){ - res.render('contact', {qs: req.query}); -}); +// Set up template engine +app.set('view engine', 'ejs'); -app.post('/contact', urlencoderParser, function(req, res){ - console.log(req.body); - res.render('contact-success', {data: req.body}); -}); +// static files +app.use(express.static('./public')); -app.get('/profile/:name', function(req, res){ - var data = {age:22, job:'net ninja', hobies:['singing', 'dancing', 'fishing']}; - res.render('profile', {person: req.params.name, data: data}); -}); +// fire controllers +todoController(app); -app.listen(3000); +// 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..4f4d2c8b --- /dev/null +++ b/controllers/todoController.js @@ -0,0 +1,15 @@ +module.exports = function(app) { + + app.get('/todo', function(req, res){ + + }); + + app.post('/todo', function(req, res){ + + }); + + app.delete('/todo', function(req, res){ + + }); + +}; diff --git a/package.json b/package.json index 7f294cfd..fa582b8c 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,8 @@ { - "name": "node-js-playlist", + "name": "todo-app", "version": "1.0.0", - "description": "A simple to do app", + "description": "A little app to use as a To Do list", "main": "app.js", - "dependencies": { - "body-parser": "^1.17.2", - "ejs": "^2.5.6", - "express": "^4.15.3" - }, - "devDependencies": {}, "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, @@ -21,5 +15,10 @@ "bugs": { "url": "https://github.com/imvpn22/node-js-playlist/issues" }, - "homepage": "https://github.com/imvpn22/node-js-playlist#readme" + "homepage": "https://github.com/imvpn22/node-js-playlist#readme", + "dependencies": { + "body-parser": "^1.17.2", + "ejs": "^2.5.6", + "express": "^4.15.3" + } } From d7ae4cb63ce770d6ed92271b41ea8581a88e3d9e Mon Sep 17 00:00:00 2001 From: imvpn22 Date: Thu, 27 Jul 2017 21:29:30 +0530 Subject: [PATCH 3/6] 3rd commit --- controllers/todoController.js | 2 +- public/assets/styles.css | 2 +- views/todo.ejs | 24 ++++++++++++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 views/todo.ejs diff --git a/controllers/todoController.js b/controllers/todoController.js index 4f4d2c8b..f679a776 100644 --- a/controllers/todoController.js +++ b/controllers/todoController.js @@ -1,7 +1,7 @@ module.exports = function(app) { app.get('/todo', function(req, res){ - + res.render('todo'); }); app.post('/todo', function(req, res){ 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/views/todo.ejs b/views/todo.ejs new file mode 100644 index 00000000..00ccb8f9 --- /dev/null +++ b/views/todo.ejs @@ -0,0 +1,24 @@ + + + + Todo List + + + + +

My todo list

+
+
+ + +
+ +
    +
  • Item 1
  • +
  • Item 2
  • +
  • Item 3
  • +
+ +
+ + From 9dc435e40afe3c7937b16054c184fa4fa6e180f1 Mon Sep 17 00:00:00 2001 From: imvpn22 Date: Thu, 27 Jul 2017 23:21:08 +0530 Subject: [PATCH 4/6] 4th commit --- controllers/todoController.js | 20 ++++++++++++++++---- public/assets/todo-list.js | 2 +- views/todo.ejs | 11 ++++++----- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/controllers/todoController.js b/controllers/todoController.js index f679a776..18884f36 100644 --- a/controllers/todoController.js +++ b/controllers/todoController.js @@ -1,15 +1,27 @@ +var bodyParser = require('body-parser'); + +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){ - res.render('todo'); + res.render('todo', {todos: data}); }); - app.post('/todo', function(req, res){ + app.post('/todo', urlencodedParser, function(req, res){ + data.push(req.body); + res.json(data); + }); + app.delete('/todo/:item', function(req, res){ + data = data.filter(function(todo){ + return todo.item.replace(/ /g, '-') !== req.params.item; + }); + res.json(data); }); - app.delete('/todo', function(req, res){ - }); + }; 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 index 00ccb8f9..d64b7f03 100644 --- a/views/todo.ejs +++ b/views/todo.ejs @@ -4,19 +4,20 @@ Todo List + -

My todo list

+

My todo list

- +
    -
  • Item 1
  • -
  • Item 2
  • -
  • Item 3
  • + <% for(var i=0; i < todos.length; i++) { %> +
  • <%= todos[i].item %>
  • + <% } %>
From 9e60a30cd4ea1fa9e544fede1ad454fc2f415fd2 Mon Sep 17 00:00:00 2001 From: imvpn22 Date: Sat, 29 Jul 2017 11:03:05 +0530 Subject: [PATCH 5/6] Commit 5: added database --- controllers/todoController.js | 36 ++++++++++++++++++++++++++++------- package.json | 4 +++- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/controllers/todoController.js b/controllers/todoController.js index 18884f36..18bfd617 100644 --- a/controllers/todoController.js +++ b/controllers/todoController.js @@ -1,24 +1,46 @@ var bodyParser = require('body-parser'); +var mongoose = require('mongoose'); -var data = [{item: 'get milk'}, {item: 'Walk dog'}, {item: 'Do some code'}]; + +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){ - res.render('todo', {todos: data}); + // 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){ - data.push(req.body); - res.json(data); + // 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){ - data = data.filter(function(todo){ - return todo.item.replace(/ /g, '-') !== req.params.item; + // 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); }); - res.json(data); }); diff --git a/package.json b/package.json index fa582b8c..8f293527 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,8 @@ "dependencies": { "body-parser": "^1.17.2", "ejs": "^2.5.6", - "express": "^4.15.3" + "express": "^4.15.3", + "mongodb": "^2.2.30", + "mongoose": "^4.11.4" } } From af90a1bc89e9796d021fcdbc0e9f9656c970181a Mon Sep 17 00:00:00 2001 From: Vipin Yadav Date: Sat, 29 Jul 2017 11:09:43 +0530 Subject: [PATCH 6/6] Create README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 00000000..f576b8ed --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Todo app +This is a web app created in Node.js, using mongodb +