Created
March 14, 2011 13:10
-
-
Save thiagofm/869088 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* Module dependencies. | |
*/ | |
var express = require('express'); | |
var app = module.exports = express.createServer(); | |
// 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.logger()); | |
app.use(app.router); | |
app.use(express.static(__dirname + '/public')); | |
}); | |
app.configure('development', function(){ | |
app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); | |
app.use(express.logger()); | |
}); | |
app.configure('production', function(){ | |
app.use(express.errorHandler()); | |
app.use(express.logger()); | |
}); | |
//mongodb | |
var mongoose = require('mongoose'); | |
var db = mongoose.connect('mongodb://localhost/mydb'); | |
var Schema = mongoose.Schema; | |
var Posts = new Schema({ | |
title : String, | |
message : String, | |
board : String | |
}); | |
mongoose.model('Post', Posts); | |
//Routes | |
app.get('/:board', function(req,res,next){ | |
var Post = mongoose.model('Post'); | |
Post.find({},function(err,posts){ | |
console.log(posts); | |
res.render('index',{ | |
title: req.params.board, | |
board: req.params.board, | |
locals: { posts: posts } | |
}); | |
}); | |
// res.render('index',{ | |
// title: req.params.board, | |
// locals: { posts: posts } | |
}); | |
app.get('/:board/new', function(req,res){ | |
res.render('post',{ | |
title: 'Create new thread' | |
}); | |
}); | |
app.post('/create', function(req, res){ | |
// res.render('index', { | |
// title: 'nota' | |
// }); | |
var BoardPost = mongoose.model('Post'); | |
var post = new BoardPost(); | |
post.title=req.body.post.title; | |
post.message = req.body.post.message; | |
post.board = req.body.post.board; | |
post.save(function(err){ | |
if(!err){ | |
res.render('sent',{ | |
title:'Posted with success.', | |
post: post | |
}) | |
;} | |
}); | |
}); | |
// Only listen on $ node app.js | |
if (!module.parent) { | |
app.listen(3000); | |
console.log("Notachan rodando na porta %d, fode sim.", app.address().port) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment