forked from madhums/node-express-mongoose-demo
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 28d800d
Showing
13 changed files
with
333 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<h1>A demo app in node.js illustrating CURD operations using Express, mongoose and Jade</h1> | ||
|
||
<p> | ||
<h3>Steps</h3> | ||
<ul> | ||
<li>Install mongodb : sudo apt-get install mongodb</li> | ||
<li>Install node.js : https://github.com/joyent/node</li> | ||
<li>Install npm : curl http://npmjs.org/install.sh | sh</li> | ||
<li>git clone git://github.com/madhums/nodejs-express-mongoose-demo.git noobjs</li> | ||
<li>cd noobjs</li> | ||
<li>npm install</li> | ||
<li>node app.js</li> | ||
<li>http://localhost:3000/</li> | ||
</ul> | ||
</p> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var express = require('express'), | ||
sys = require('sys'), | ||
app = module.exports = express.createServer(), | ||
models = require('./model'); | ||
Article = models.Article; | ||
|
||
// Configuration | ||
|
||
app.configure(function(){ | ||
app.set('views', __dirname + '/views'); | ||
app.set('view engine', 'jade'); | ||
app.use(express.bodyParser()); | ||
app.use(express.methodOverride()); | ||
app.use(express.cookieParser()); | ||
app.use(express.session({ secret: 'your secret here' })); | ||
app.use(express.compiler({ src: __dirname + '/public', enable: ['sass'] })); | ||
app.use(app.router); | ||
app.use(express.static(__dirname + '/public')); | ||
}); | ||
|
||
app.configure('development', function(){ | ||
app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); | ||
}); | ||
|
||
app.configure('production', function(){ | ||
app.use(express.errorHandler()); | ||
}); | ||
|
||
|
||
|
||
// Root | ||
|
||
app.get('/', function(req, res){ | ||
res.redirect('/articles'); | ||
}); | ||
|
||
|
||
// Article Routes | ||
|
||
// Listing of Articles | ||
app.get('/articles', function(req, res){ | ||
Article.find({}, function(err, docs) { | ||
res.render('articles/index', { | ||
title: 'List of Articles', | ||
articles: docs | ||
}); | ||
}); | ||
}); | ||
|
||
// New article | ||
app.get('/articles/new', function(req, res){ | ||
res.render('articles/new', { | ||
title: 'New Article' | ||
}); | ||
}); | ||
|
||
// Create/Update articles | ||
app.post('/articles', function(req, res){ | ||
if(req.body.article._id) | ||
Article.findOne({_id:req.body.article._id}, function(err, a) { | ||
a.title = req.body.article.title; | ||
a.body = req.body.article.body; | ||
a.save(function(err) { | ||
console.log(err); | ||
}) | ||
}); | ||
else { | ||
article = new Article(req.body.article); | ||
article.save(function(err){ | ||
console.log("Created"); | ||
}); | ||
} | ||
|
||
res.redirect('/articles'); | ||
|
||
}); | ||
|
||
// View an article | ||
app.get('/article/:id', function(req, res){ | ||
Article.findOne({_id:req.params.id}, function(err,article){ | ||
res.render('articles/show', { | ||
title: article.doc.title, | ||
article: article.doc | ||
}); | ||
}); | ||
}); | ||
|
||
// Edit an article | ||
app.get('/article/:id/edit', function(req, res){ | ||
Article.findOne({_id:req.params.id}, function(err,article){ | ||
res.render('articles/edit', { | ||
title: 'Edit '+article.doc.title, | ||
article: article.doc | ||
}); | ||
}); | ||
}); | ||
|
||
// Delete an article | ||
app.del('/article/:id', function(req, res){ | ||
Article.findOne({_id:req.params.id}, function(err,article){ | ||
article.remove(function(err){ | ||
console.log(err); | ||
}); | ||
}); | ||
res.redirect('/articles'); | ||
}); | ||
|
||
|
||
|
||
|
||
|
||
// Only listen on $ node app.js | ||
|
||
if (!module.parent) { | ||
app.listen(3000); | ||
console.log("Express server listening on port %d", app.address().port) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
var mongoose = require('mongoose'); | ||
mongoose.connect('mongodb://localhost/articlesdb'); | ||
Schema = mongoose.Schema, | ||
ObjectId = Schema.ObjectId; | ||
|
||
// Article schema | ||
|
||
var Article = new Schema({ | ||
title : {type : String, default : '', required : true}, | ||
body : {type : String, default : ''}, | ||
created_at : {type : Date, default : Date.now}, | ||
updated_at : {type : Date, default : Date.now} | ||
}); | ||
|
||
mongoose.model('Article', Article); | ||
var Article = exports.Article = mongoose.model('Article'); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "noobjs", | ||
"description": "A demo app in nodejs illustrating use of express, jade and mogoose", | ||
"version": "0.0.1", | ||
"homepage": "", | ||
"author": "Madhusudhan M S (http://twitter.com/madhums)", | ||
"directories": { | ||
"public": "./public" | ||
}, | ||
"engines": { | ||
"node": ">= 0.4.2" | ||
}, | ||
"dependencies": { | ||
"express": "2.0.0beta", | ||
"mongoose": "1.1.2", | ||
"jade": "0.6.3" | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
body { | ||
padding: 50px; | ||
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif; | ||
color: #444;} | ||
|
||
a { | ||
color: #00B7FF;} | ||
|
||
textarea { | ||
width: 400px; | ||
height: 200px;} | ||
|
||
input[type=text] { | ||
width: 400px;} | ||
|
||
textarea, input[type=text] { | ||
padding: 5px; | ||
border: 1px solid #bbb;} | ||
|
||
* { | ||
outline: none;} | ||
|
||
#article { | ||
padding: 15px; | ||
border-bottom: 1px solid #e0e0e0;} | ||
|
||
#article_body { | ||
padding: 5px 0;} | ||
|
||
#article_title { | ||
font-size: 16px; | ||
font-weight: bold;} | ||
|
||
#article_date { | ||
color: #aaa; | ||
font-size: 12px;} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
body | ||
:padding 50px | ||
:font 14px "Lucida Grande", Helvetica, Arial, sans-serif | ||
:color #444 | ||
a | ||
:color #00B7FF | ||
|
||
textarea | ||
:width 400px | ||
:height 200px | ||
|
||
input[type=text] | ||
:width 400px | ||
|
||
textarea, input[type=text] | ||
:padding 5px | ||
:border 1px solid #bbb | ||
|
||
* | ||
:outline none | ||
|
||
#article | ||
:padding 15px | ||
:border-bottom 1px solid #e0e0e0 | ||
|
||
#article_body | ||
:padding 5px 0 | ||
|
||
#article_title | ||
:font-size 16px | ||
:font-weight bold | ||
|
||
#article_date | ||
:color #aaa | ||
:font-size 12px |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
|
||
// Run $ expresso | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var app = require('../app') | ||
, assert = require('assert'); | ||
|
||
|
||
module.exports = { | ||
'GET /': function(){ | ||
assert.response(app, | ||
{ url: '/' }, | ||
{ status: 200, headers: { 'Content-Type': 'text/html; charset=utf-8' }}, | ||
function(res){ | ||
assert.includes(res.body, '<title>Express</title>'); | ||
}); | ||
} | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
h1= title | ||
|
||
form(method="post",action="/articles") | ||
p | ||
label Title | ||
br | ||
input(type="text", name="article[title]", value=article.title) | ||
|
||
p | ||
label Body | ||
br | ||
textarea(name="article[body]") #{article.body} | ||
|
||
input(type="hidden", name="article[_id]", value=article._id) | ||
|
||
input(type="submit", value="Submit") | ||
|
||
a(href='/articles') Cancel | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
h1= title | ||
p #{title} | ||
a(href="/articles/new") New Article | ||
hr | ||
div#articles | ||
- each article in articles | ||
div#article | ||
div#article_title | ||
a(href='/article/'+article._id) #{article.title} | ||
div#article_body | ||
=article.body | ||
a(href='/article/'+article._id+'/edit') Edit |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
h1= title | ||
p Create a new article | ||
|
||
form(method="post",action="/articles") | ||
p | ||
label Title | ||
br | ||
input(type="text", name="article[title]") | ||
|
||
p | ||
label Body | ||
br | ||
textarea(name="article[body]") | ||
|
||
input(type="submit", value="Submit") |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
h1= title | ||
|
||
p #{article.body} | ||
- var month_names = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); | ||
- day = article.created_at.getDate() | ||
- month = article.created_at.getMonth() | ||
- year = article.created_at.getFullYear() | ||
|
||
p#article_date | ||
created on #{month_names[month]} #{day}, #{year} | ||
|
||
p | ||
a(href='/article/'+article._id+'/edit') Edit | ||
|
||
p | ||
a(href='/articles') Back |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
h1= title | ||
p Welcome to #{title} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
!!! | ||
html | ||
head | ||
title= title | ||
link(rel='stylesheet', href='/stylesheets/style.css') | ||
body!= body |