如何实施Restify server.put?

时间:2015-10-18 23:00:08

标签: node.js restful-architecture restify

您好我已经开始使用Restify开发Restful API但我在开发[PUT]时遇到了问题。

我的想法是获取参数并使用新的参数更改它。但是这段代码似乎不起作用。请帮帮我:D

server.put('/user/:_id', function (req, res, next) {
  var user = {};
  user._id = req.params._id;
  console.log('_id ===  '+user);
  var changes = req.params;
  delete changes._id;
  // delete changes._id;
  for(var x in changes) {
    user[x] = changes[x];
    console.log('user[x] ='+user[x]);
  }
  db.Student.update(req.params._id, user,{multi:true,upsert:true}, function(err,data){
  res.writeHead(200, {'Content-Type': 'application/json; charset=utf-8'});
  res.end(JSON.stringify(user));

  return next();
});
});

这里是putclient

var restify = require('restify');
var server = require('./app');

var client = restify.createJsonClient({
    url: 'http://localhost:3000'
});

var testUser = {
  $set:
  {
    "name" : "procopio magalpok",
    "yearLevel" : "IX"
  }
};

 testUser._id = "561de16f885fb2f40d23ece",

 client.put('/user/'+testUser._id, testUser , function (err, req, res, status) {
    if (err) {
        console.log("An error ocurred >>>>>>");
        console.log(err);
    } else {
        console.log('id : '+testUser._id);
        console.log('User updated >>>>>>>');
        console.log(status);
    }

});

0 个答案:

没有答案