【问题标题】:Restify - how to make post reqRestify - 如何使 post req
【发布时间】:2018-01-12 19:58:32
【问题描述】:

我正在尝试使用 Restify 创建一个 API,但我无法读取请求的正文。

这是代码

main.js:

'use strict';


const perfy = require('perfy');

perfy.start('startup');


const restify     = require('restify');
const operation        = require('./operation');

// post call for redirect
server.post('/test', operation.status);

操作.js:

'use strict';



module.exports =
{
    /**
     *
     * @param {Object} req  The request object as received from the middleware
     * @param {Object} res  The response object as received from the middleware
     * @param {Function} next The next function to be called by the middleware
     */
    status: function(req, res, next)
    {
        // Check configuration loading:
        CONF.get()
        .then( SETTINGS =>
        {

                req.log.debug('[TRACKER] The tracker has been requested to send the following data: %s', req.body);

                // Then return 200 if everything is OK:
                res.send(
                {
                    status: 'OK!'
                });

                next();

        })
        .catch( err =>
        {
            // Return server error:
            next(new restify.InternalError('Could not load configuration. ' + err));
        });
    }

 };

在邮递员中,我在 POST 中调用服务:

Postman POST

然后,它接收到帖子(返回状态:ok)但记录器无法查看正文。未定义:

这是日志的结果: "[TRACKER] 已请求跟踪器发送以下数据:undefined","time":"2018-01-11T12:06:13.133Z","v":0}

我哪里做错了?

【问题讨论】:

    标签: javascript node.js post server restify


    【解决方案1】:

    通过server.use(restify.bodyParser());解决

    在 main.js 中

    'use strict';
    
    
    const perfy = require('perfy');
    
    perfy.start('startup');
    
    
    const restify     = require('restify');
    const operation        = require('./operation');
    
    server.use(restify.bodyParser());
    
    // post call for redirect
    server.post('/test', operation.status);
    

    更多信息 -> RESTify on Node.js POST body / json

    【讨论】:

      猜你喜欢
      • 2021-08-31
      • 1970-01-01
      • 1970-01-01
      • 2014-04-08
      • 2018-02-04
      • 2017-03-23
      • 2015-02-10
      • 1970-01-01
      • 2020-01-05
      相关资源
      最近更新 更多