Skip to content

Instantly share code, notes, and snippets.

@corehook
Forked from fwielstra/api.js
Created March 27, 2017 09:09
Show Gist options
  • Select an option

  • Save corehook/0bca071c3c61d1b058e15d4b8740bbaf to your computer and use it in GitHub Desktop.

Select an option

Save corehook/0bca071c3c61d1b058e15d4b8740bbaf to your computer and use it in GitHub Desktop.

Revisions

  1. @fwielstra fwielstra revised this gist Jun 15, 2011. 2 changed files with 4 additions and 4 deletions.
    6 changes: 3 additions & 3 deletions api.js
    Original file line number Diff line number Diff line change
    @@ -14,9 +14,9 @@ exports.post = function(req, res) {
    }

    exports.list = function(req, res) {
    Thread.find(function(err, threads) {
    res.send(threads);
    });
    Thread.find(function(err, threads) {
    res.send(threads);
    });
    }

    // first locates a thread by title, then locates the replies by thread ID.
    2 changes: 1 addition & 1 deletion app.js
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@ app.configure(function(){
    app.use(app.router);
    });

    // set up the RESTful API, handler methods are defined in thread.js
    // 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);
  2. @fwielstra fwielstra revised this gist Jun 14, 2011. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions app.js
    Original file line number Diff line number Diff line change
    @@ -14,10 +14,10 @@ app.configure(function(){
    });

    // set up the RESTful API, handler methods are defined in thread.js
    var thread = require('./controllers/thread.js');
    app.post('/thread', thread.post);
    app.get('/thread/:title.:format?', thread.show);
    app.get('/thread', thread.list);
    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);
  3. @fwielstra fwielstra created this gist Jun 14, 2011.
    29 changes: 29 additions & 0 deletions api.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    /* 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}]);
    });
    })
    });
    23 changes: 23 additions & 0 deletions app.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    // 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 thread.js
    var thread = require('./controllers/thread.js');
    app.post('/thread', thread.post);
    app.get('/thread/:title.:format?', thread.show);
    app.get('/thread', thread.list);

    app.listen(3000);
    console.log("Express server listening on port %d", app.address().port);
    14 changes: 14 additions & 0 deletions post.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    // 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);
    12 changes: 12 additions & 0 deletions thread.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    // 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);