title: How to best create a RESTful API in Node.js
body: <p>I'm a beginner in Node (and generally all back-end web development), and I have started to write a RESTful API in Node. There are a few things I'm trying to get my head around.</p>
  
  <p>My application uses Express and Mongoose, and I am using the <code>express-resource</code> module to easily create my CRUD routes for the API resources. But there are a couple of things I am unhappy about, and think I could do better.</p>
  
  <p>The first is Mongoose. If I want to write tests for my API, I have no way of stubbing Mongoose to force it to in memory data. All of the tutorials out there seem to point to Mongoose, however, and I'm really not sure what I should be using.</p>
  
  <p>Secondly, my resource seems to have a lot of boilerplate code. Is this really the best way to create a RESTful API in Node.js? Are there other modules that will help me to create my CRUD routes? I believe there are ways you can create CRUD routes right from your schema, without anymore code, but I'm really not sure how.</p>
  
  <p>I have seen projects out there such as Tower.js and CompoundJS (formally RailwayJS) that seem to be these comprehensive solutions that solve much more than my issues here. Perhaps I should be using them, but I really only want the Node.js application to be an API and nothing more. I am dealing with the front-end independently of the API.</p>
  
  <p>To provide some context, here is my current situation. Currently, I have a model defined in Mongoose:</p>
  
  <pre><code>var mongoose = require('mongoose')
    , Schema = mongoose.Schema
    , Link
  
  var LinkSchema = new Schema({
    uri: String,
    meta: {
      title: String,
      desc: String
    },
    shares: [{
      uid: Schema.Types.ObjectId,
      date: Date,
      message: String
    }]
  })
  
  Link = module.exports = mongoose.model('Link')
  </code></pre>
  
  <p>Next, I define the controllers for the CRUD routes:</p>
  
  <pre><code>var mongoose = require('mongoose')
    , _ = require('underscore')
    , Link = mongoose.model('Link')
  
  exports.load = function (req, id, fn) {
    Link.findById(req.params.link, function (err, link) {
      if (err) {
        return res.send(err)
      }
  
      fn(null, link)
    })
  }
  
  exports.index = function (req, res) {
    var filterByUser = req.query.user ? { 'shares.uid': req.query.user } : {}
  
    Link.find(filterByUser, function (err, links) {
      if (err) {
        return res.send(err)
      }
  
      res.send(links)
    })
  }
  
  exports.create = function (req, res) {
    var link = new Link(req.body)
  
    link.save(function (err) {
      if (err) {
        // TODO: send 404
        return res.send(err)
      }
  
      res.send(link)
    })
  }
  
  exports.show = function (req, res) {
    res.send(req.link)
  }
  
  exports.update = function (req, res) {
    req.link = _(req.link).extend(req.body)
  
    req.link.save(function (err, link) {
      if (err) {
        return res.send(err)
      }
  
      res.send(link)
    })
  }
  
  exports.patch = exports.update
  
  exports.destroy = function (req, res) {
    req.link.remove(function (err) {
      if (err) {
        return res.send(err)
      }
  
      res.send()
    })
  }
  </code></pre>
  
  <p>Finally, I use the <code>express-resource</code> module to map these controllers to the necessary CRUD routes on top of the Express app.</p>
  
  <pre><code>app.resource('api/links', require('../resources/links'))
  </code></pre>