Skip to content

Instantly share code, notes, and snippets.

@riconegri
Forked from fwielstra/api.js
Created October 25, 2012 23:13
Show Gist options
  • Save riconegri/3956052 to your computer and use it in GitHub Desktop.
Save riconegri/3956052 to your computer and use it in GitHub Desktop.
An example NodeJS / Mongoose / Express application based on their respective tutorials
/* The API controller
Exports 3 methods:
* post - Creates a new thread
* list - Returns a list of threads
* show - Displays a thread and its posts
*/
var Thread = require('../models/thread.js');
var Post = require('../models/post.js');
exports.post = function(req, res) {
new Thread({title: req.body.title, author: req.body.author}).save();
}
exports.list = function(req, res) {
Thread.find(function(err, threads) {
res.send(threads);
});
}
// first locates a thread by title, then locates the replies by thread ID.
exports.show = (function(req, res) {
Thread.findOne({title: req.params.title}, function(error, thread) {
var posts = Post.find({thread: thread._id}, function(error, posts) {
res.send([{thread: thread, posts: posts}]);
});
})
});
// The main application script, ties everything together.
var express = require('express');
var mongoose = require('mongoose');
var app = module.exports = express.createServer();
// connect to Mongo when the app initializes
mongoose.connect('mongodb://localhost/norum');
app.configure(function(){
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
});
// set up the RESTful API, handler methods are defined in api.js
var api = require('./controllers/api.js');
app.post('/thread', api.post);
app.get('/thread/:title.:format?', api.show);
app.get('/thread', api.list);
app.listen(3000);
console.log("Express server listening on port %d", app.address().port);
{
"name": "express",
"description": "Sinatra inspired web development framework",
"version": "3.0.0",
"author": "TJ Holowaychuk <tj@vision-media.ca>",
"contributors": [
{ "name": "TJ Holowaychuk", "email": "tj@vision-media.ca" },
{ "name": "Aaron Heckmann", "email": "aaron.heckmann+github@gmail.com" },
{ "name": "Ciaran Jessup", "email": "ciaranj@gmail.com" },
{ "name": "Guillermo Rauch", "email": "rauchg@gmail.com" }
],
"dependencies": {
"connect": "2.6.0",
"commander": "0.6.1",
"range-parser": "0.0.4",
"mkdirp": "0.3.3",
"cookie": "0.0.4",
"crc": "0.2.0",
"fresh": "0.1.0",
"methods": "0.0.1",
"send": "0.1.0",
"debug": "*"
},
"devDependencies": {
"ejs": "*",
"mocha": "*",
"jade": "*",
"hjs": "*",
"stylus": "*",
"should": "*",
"connect-redis": "*",
"github-flavored-markdown": "*",
"supertest": "0.0.1"
},
"keywords": [
"express",
"framework",
"sinatra",
"web",
"rest",
"restful",
"router",
"app",
"api"
],
"repository": "git://github.com/visionmedia/express",
"main": "index",
"bin": { "express": "./bin/express" },
"scripts": {
"prepublish" : "npm prune",
"test": "make test"
},
"engines": { "node": "*" }
}
// The Post model
var mongoose = require('mongoose')
,Schema = mongoose.Schema
,ObjectId = Schema.ObjectId;
var postSchema = new Schema({
thread: ObjectId,
date: {type: Date, default: Date.now},
author: {type: String, default: 'Anon'},
post: String
});
module.exports = mongoose.model('Post', postSchema);
// The Thread model
var mongoose = require('mongoose')
, Schema = mongoose.Schema;
var threadSchema = new Schema({
title: String,
postdate: {type: Date, default: Date.now},
author: {type: String, default: 'Anon'}
});
module.exports = mongoose.model('Thread', threadSchema);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment