Simplest REST API I've coded

I needed a quick REST API for a project I was working on in Node and this really fit the need.  Was really surprised at how easy it was. The app.param function was extremely useful.

schema.js

var mongoose = require(“mongoose”),

Schema = mongoose.Schema;

var Network = new Schema({

    Name: {

        type: String,

index: true

    },

    Code: {

        type: String

    }

})

mongoose.model(‘Network’, Network, “network”);

server.js

var express = require('express’)

    , app = module.exports = express.createServer()

    , mongoose = require(“mongoose”);

// Configuration

app.configure(function() {

    app.set('views’, __dirname + ’/views’);

    app.set('view engine’, 'jade’);

    app.use(express.bodyParser());

    app.use(express.methodOverride());

    app.use(app.router);

    app.use(express.static(__dirname + ’/’));

});

app.configure('development’,

function() {

    app.use(express.errorHandler({

        dumpExceptions: true,

        showStack: true

    }));

});

app.configure('production’,

function() {

    app.use(express.errorHandler());

});

var db = mongoose.connect(require(’./config’).mongoConfig,

function(err) {

    if (err) {

        console.log('err’);

        throw err;

    }

});

require(’./schema’);

var Network = db.model('Network’, Network, 'network’);

// Params

app.param('code’,

function(req, res, next, code) {

    req.code = code;

    next();

});

// Routes

app.get(’/networks’,

function(req, res) {

    Network.find({}).sort('Code’, 'ascending’).execFind(

    function(err, networks) {

        if (err) {

            throw err;

        }

        res.contentType('application/json’);

        res.json(networks);

    });

});

app.get(’/networks/:id’,

function(req, res) {

    Network.findById(req.params.id,

    function(err, networks) {

        if (err) {

            throw err;

        }

        res.contentType('application/json’);

        res.json(networks);

    });

});

app.get(’/networks/:code’,

function(req, res) {

    Network.find({

        'Code’: req.code

    },

    function(err, networks) {

        if (err) {

            throw err;

        }

        res.contentType('application/json’);

        res.json(networks);

    });

});

app.listen(3000);

console.log(“Express server listening on port %d in %s mode”, app.address().port, app.settings.env);