William Jiang

JavaScript,PHP,Node,Perl,LAMP Web Developer – http://williamjxj.com; https://github.com/williamjxj?tab=repositories

Node.js REST APIs example

Node.js REST APIs example

The following is a very basic and standard REST API example, with the require(‘mongoose’) in expressjs.

var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;

// required: true,
var Task = new Schema({
  task : String,
  created_at: { type: Date, default: Date.now },
  updated_at : Date
});

var Task = mongoose.model('Task', Task);
...

app.get('/api/tasks', function(req, res, next){
  Task.find({}, function (err, docs) {
    res.json(docs);
  });
});

app.post('/api/tasks', function(req, res){
  var doc = new Task(req.body.task);
  doc.save(function (err) {
    if (!err) {
      res.json(doc);
    } else {
      res.send(err, 422);
    }
  });
});

app.get('/api/tasks/:id', function(req, res){
  Task.findById(req.params.id, function (err, doc){
    if (doc) {
      res.json(doc);
    } else {
      res.json(404);
    }
  });
});

app.put('/api/tasks/:id', function(req, res){
  Task.findById(req.params.id, function (err, doc){
    if (!doc) {
      res.json(404)
    } else {
      doc.updated_at = new Date();
      doc.task = req.body.task.task;
      doc.save(function (err) {
        if (!err) {
          res.json(doc);
        } else {
          res.send(err, 422);
        }
      });
    }
  });
});

app.del('/api/tasks/:id', function(req, res){
  Task.findById(req.params.id, function (err, doc){
    if (doc) {
      doc.remove(function() {
        res.json(200)
      });
    } else {
      res.json(404)
    }
  });
});

The calling is like:

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

%d bloggers like this: