【问题标题】:Posting data to restify server from jsonClient is not working从 jsonClient 将数据发布到 restify 服务器不起作用
【发布时间】:2014-11-09 08:41:25
【问题描述】:

我正在使用 restify,对于 TDD,我正在使用 mocha 测试框架。当我使用 post 方法从 restify 客户端测试 restify 服务器时,它没有按预期工作。

我的服务器代码如下:

exports.post_user = function(req,res,next) {
var body = req.body;
if (!body.name || !body.email) {
    throw Error(JSON.stringify(body));
}
// Checking duplicate user before save user information into database
User.findOne({ email: body.email }, function (err, user) {
    if(err) { req.log.error(err); }
    if (!err && user != null) {
        res.send(user);
    } else {

        var user = new User({
            oauthID: Math.random(),
            name: body.name,
            email: body.email,
            created : Date.now()
        });

        // Profile not available then save information and return response
        user.save(function(err) {
            if (err) {
                console.log(err);
                req.log.error(err);
            } else {
                res.send(user);
            };
        });
    };
});

};

我的测试代码如下

describe('core', function () {
describe('/admin/user', function () {
    it('POST with data', function (done) {
        var data = { 
            name: "aruljoth1i", 
            email: "aruljot1h1iparthiban@hotmail.com"
        };
        client.post('/admin/user', data, function (err, req, res, obj) {
            assert.equal(res.statusCode, 200,err);
            done();
        });
    });
});
});

当我将数据作为 {name:'aruljothi'} 传递时,它正在工作,但与上述 json 对象的情况一样,它不起作用。在服务器中 req.body 以 {} 的形式出现。

错误

 2) Expecting 200 status code post /admin/user -> should return 200 status:
 Uncaught
 AssertionError: InternalServerError: {"message":"{}"}

提前谢谢你。

【问题讨论】:

    标签: node.js mocha.js restify


    【解决方案1】:

    请查看此网址http://mcavage.me/node-restify/#JsonClient

    client.post('/foo', { hello: 'world' }, function(err, req, res, obj) {
      assert.ifError(err);
      console.log('%d -> %j', res.statusCode, res.headers);
      console.log('%j', obj);
    });
    

    【讨论】:

    • 很抱歉,这个文档引用对我没有任何帮助,因为它没有指出 @JOTHI 缺少什么。
    【解决方案2】:

    我遇到了这个问题,因为通过 restify 的 JsonClient 发送对象 o 导致服务器端出现空对象 {}

    把它放在代码中,如果是这样的

    var express = require('express');
    var bodyParser = require('body-parser')
    
    var app = express();
    app.use(bodyParser.urlencoded())
    app.listen(12345, function() {
        app.post('/', function(req, res) {
            console.log(req.body);
            res.end();
        })
    })
    
    var restify = require('restify');
    var client = restify.createJsonClient({
            url: 'http://localhost:12345',
            version: '*',
        });
    
    var some_object = {'foo': 'bar', '42': 23}
    client.post('/', some_object, function(err, req, res, obj) {
    });
    

    req.body 提供{},确保同时使用json bodyparser 作为中间件

    app.use(bodyParser.json());
    

    那么它最终应该会给你想要的{ '42': 23, foo: 'bar' }

    【讨论】:

    • 我很确定@JOTHI 这可能会解决您的问题,但我无法理解您的示例。如果我的适合您的情况,请随时将其复制到您的问题中。
    • 使用 restify 的 StringClient 并作为替代发送 JSON.stringify(o) 也没有按预期工作,因为它给了我 o 的字符串化版本,包裹在一个看起来像 { 'stringified o so to say': ''} 的对象中。跨度>
    【解决方案3】:

    我使用 JSON.parse 解决了完全相同的问题,由于某种原因,var 数据没有被识别为对象。

    describe('core', function () {
    describe('/admin/user', function () {
        it('POST with data', function (done) {
            var data = { 
                name: "aruljoth1i", 
                email: "aruljot1h1iparthiban@hotmail.com"
            };
            data = JSON.parse(data);
            client.post('/admin/user', data, function (err, req, res, obj) {
                assert.equal(res.statusCode, 200,err);
                done();
            });
        });
    });
    });
    

    【讨论】:

      猜你喜欢
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 2021-04-10
      • 2019-09-10
      • 2012-03-29
      • 2021-06-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多