build rest api with node and express js

1. in the app js we need

 

app.get('/api/members', restImpl.findAll);
app.get('/api/members/:id', restImpl.findById);
app.post('/api/addMember', restImpl.addMember);
app.put('/api/members/:id', restImpl.updateMember);
app.delete('/api/members/:id', restImpl.deleteMember);

2. To share the mongo db connection we can have the db info in a js file:

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/contacts');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));

module.exports = db;

3. To share the model schema, we can put the content in a js file:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var MemberSchema = Schema({
    name: {
        first: String,
        last: String
    },
    address: String,
    phone: String
});

//register Schema for model "Member".
mongoose.model('Member',MemberSchema);

module.exports = MemberSchema;

4. then in the rest implementation js module :

var mongoose = require('mongoose');
var database = require('../model/database');
var memberSchema = require('../model/Member').MemberSchema;

exports.findAll = function (req, res) {
    var db = database.db;
    var Member = mongoose.model('Member', memberSchema);
    Member.find(function (err, results) {
        dbCallback(err, results, res);
    });
};

exports.findById = function (req, res) {
    var db = database.db;
    var Member = mongoose.model('Member', memberSchema);
    var id = req.params.id;
    Member.findById(id, function (err, results) {
        dbCallback(err, results, res);
    });
};
exports.addMember = function (req, res) {
    var db = database.db;
    var Member = mongoose.model('Member', memberSchema);
    var memberToSave = new Member(req.body);
    memberToSave.save(function (err, results) {
        dbCallback(err, results, res);
    });
};

exports.updateMember = function (req, res) {
    var db = database.db;
    var Member = mongoose.model('Member', memberSchema);
    var id = req.params.id;
    Member.findByIdAndUpdate(id, req.body, function (err, results) {
        dbCallback(err, results, res);
    });
};

exports.deleteMember = function (req, res) {
    var db = database.db;
    var Member = mongoose.model('Member', memberSchema);
    var id = req.params.id;
    Member.findByIdAndRemove(id, function (err, results) {
        dbCallback(err, results, res);
    });
};

var dbCallback = function (err, results, res) {
    if (err) return console.error(err);
    console.log(results);
    res.json(results);
}

DONE.

To test them , remember to set the Content-Type to application/js in the header if you do post/put.

Also, supertest or mocha are nice tool for testing. supertest is very popular. mocha’s author wrote express, so they should be very easy to be used together.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s