Created
August 26, 2014 20:35
-
-
Save dhirajbajaj/c6140f05d92b88478fa7 to your computer and use it in GitHub Desktop.
TODO APP files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('./db'); | |
var express = require('express'); | |
var path = require('path'); | |
var favicon = require('static-favicon'); | |
var logger = require('morgan'); | |
var cookieParser = require('cookie-parser'); | |
var bodyParser = require('body-parser'); | |
var routes = require('./routes/index'); | |
// var users = require('./routes/users'); | |
var app = express(); | |
// view engine setup | |
app.set('views', path.join(__dirname, 'views')); | |
app.set('view engine', 'jade'); | |
app.use(favicon()); | |
app.use(logger('dev')); | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded()); | |
app.use(cookieParser()); | |
app.use(express.static(path.join(__dirname, 'public'))); | |
app.use('/', routes); | |
// app.use('/users', users); | |
/// catch 404 and forward to error handler | |
app.use(function(req, res, next) { | |
var err = new Error('Not Found'); | |
err.status = 404; | |
next(err); | |
}); | |
/// error handlers | |
// development error handler | |
// will print stacktrace | |
if (app.get('env') === 'development') { | |
app.use(function(err, req, res, next) { | |
res.status(err.status || 500); | |
res.render('error', { | |
message: err.message, | |
error: err | |
}); | |
}); | |
} | |
// production error handler | |
// no stacktraces leaked to user | |
app.use(function(err, req, res, next) { | |
res.status(err.status || 500); | |
res.render('error', { | |
message: err.message, | |
error: {} | |
}); | |
}); | |
module.exports = app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongoose = require( 'mongoose' ); | |
var Schema = mongoose.Schema; | |
var Todo = new Schema({ | |
content : { type: String, required: true }, | |
user_id : String, | |
updated_at : Date | |
}); | |
mongoose.model( 'Todo', Todo ); | |
mongoose.connect( 'mongodb://localhost/express-myapp' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// File : views/edit.jade | |
extends layout | |
block content | |
div.page-header | |
form.form-horizontal(action="/update", method="post") | |
div.form-group.col-sm-10 | |
input.form-control.hidden(value="#{todo._id}", name="id") | |
input.form-control(type="text", name="content", placeholder="Content", value="#{todo.content}") | |
div.form-group | |
button.btn.btn-default(type="submit") Update |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// File : views/index.jade | |
extends layout | |
block content | |
- each todo in todos | |
div.panel.panel-default | |
div.panel-body | |
h3= todo.content | |
small.pull-right | |
a(href='/ed/#{todo._id}') edit | |
| |
a(href='/del/#{todo._id}') delete |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// File : routes/index.js | |
var express = require('express'); | |
var router = express.Router(); | |
var mongoose = require( 'mongoose' ); | |
var Todo = mongoose.model( 'Todo' ); | |
/* GET home page. */ | |
router.get('/', function(req, res) { | |
// show all todos relative to user | |
Todo.find().sort('-updated_at').find( function ( err, todos, count ){ | |
res.render( 'index', { | |
title : ': ALL', | |
todos : todos | |
}); | |
}); | |
}); | |
// new todo form | |
router.get('/new', function(req, res) { | |
res.render('new', { title: ': new' }); | |
}); | |
// edit todo form | |
router.get('/ed/:id', function(req, res) { | |
Todo.findById(req.params.id, function (err, todo) { | |
res.render('edit', { | |
title: ': edit', | |
todo : todo | |
}); | |
}); | |
}); | |
// update todo form | |
router.post('/update', function(req, res) { | |
console.dir(req.body); | |
Todo.findById(req.body.id, function (err, todo) { | |
todo.content = req.body.content; | |
todo.save( function ( err, todo ){ | |
res.redirect( '/' ); | |
}); | |
}); | |
}); | |
// delete todo | |
router.get('/del/:id', function(req, res) { | |
Todo.findById(req.params.id, function (err, todo) { | |
todo.remove( function ( err, todo ){ | |
res.redirect( '/' ); | |
}); | |
}); | |
}); | |
//create new todo | |
router.post('/create', function(req, res) { | |
// add TODO's to db | |
new Todo({ | |
content : req.body.content, | |
updated_at : Date.now() | |
}).save( function( err, todo, count ){ | |
res.redirect( '/' ); | |
}); | |
}); | |
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// File : views/layout.jade | |
doctype html | |
html | |
head | |
meta(name='viewport', content='width=device-width, initial-scale=1') | |
title #TODOS #{title} | |
link(href="/stylesheets/bootstrap.min.css", rel="stylesheet") | |
link(rel='stylesheet', href='/stylesheets/style.css') | |
body | |
nav.navbar.navbar-default.navbar-fixed-top(role="navigation") | |
.container | |
.navbar-header | |
button(type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse") | |
span.sr-only Toggle navigation | |
span.icon-bar | |
span.icon-bar | |
span.icon-bar | |
a.navbar-brand(href="/") #TODOS #{title} | |
.collapse.navbar-collapse.navbar-ex1-collapse | |
ul.nav.navbar-nav.navbar-right | |
li | |
a(href="/new") ADD | |
.container(style= 'padding-top:60px;' ) | |
block content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// File : views/new.jade | |
extends layout | |
block content | |
div.page-header | |
form.form-horizontal(action="/create", method="post") | |
div.form-group.col-sm-10 | |
input.form-control(type="text", name="content", placeholder="Content") | |
div.form-group | |
button.btn.btn-default(type="submit") Add |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "myapp", | |
"version": "0.0.1", | |
"private": true, | |
"scripts": { | |
"start": "node ./bin/www" | |
}, | |
"dependencies": { | |
"express": "~4.2.0", | |
"static-favicon": "~1.0.0", | |
"morgan": "~1.0.0", | |
"cookie-parser": "~1.0.1", | |
"body-parser": "~1.0.0", | |
"debug": "~0.7.4", | |
"jade": "~1.3.0", | |
"mongoose": "*" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment