Skip to content

Instantly share code, notes, and snippets.

@sreeix
Created April 11, 2011 22:42
Show Gist options
  • Save sreeix/914529 to your computer and use it in GitHub Desktop.
Save sreeix/914529 to your computer and use it in GitHub Desktop.
Posts example using express jade and alfred
var express = require('express'),
http = require('http'),
sys = require('sys'),
fs = require('fs'),
path = require('path'),
alfred = require('alfred');
var app = express.createServer();
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.static(__dirname + '/public'));
var Post;
if (!path.existsSync("posts")){
fs.mkdirSync(path.normalize("posts"), 0777);
}
alfred.open('posts', function(err, db) {
if(!err){
Post = db.define('Post');
Post.property('name');
Post.property('description');
Post.property('details');
Post.index('name', function(post) {
return post.name;
});
} else {
sys.puts(err);
}
});
app.get('/', function(req, res){
Post.find({name:{$neq: ''}}).all(function(posts){
sys.puts(sys.inspect(posts));
res.render('index', {count: posts.length, posts: posts});
});
});
app.post('/store', function(req, res){
var data = req.body;
sys.puts(sys.inspect(data));
var post = Post.new(data);
post.save(function(err){
if (err) {
console.log('Did not save because of some validation errors:');
console.log(err);
} else {
console.log('Post saved');
}
});
res.writeHead(201,{"Location": "Whatever"});
res.end();
});
app.listen(process.env.PORT || 8001);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment