Restify - 如何发布要求

时间:2018-01-11 12:15:35

标签: javascript node.js post server restify

我正在尝试使用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);

operation.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中调用该服务:

File.ReadDirNames

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

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

我在哪里做错了?

1 个答案:

答案 0 :(得分:0)

已解决server.use(restify.bodyParser());

主页上的

'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