【问题标题】:Posting request to node.js service results in timeout向 node.js 服务发送请求导致超时
【发布时间】:2015-09-06 18:41:36
【问题描述】:

深入了解 node.js 并开发了一个简单的服务:

// set up ======================================================================
var express = require('express');
var app = express();                               // create our app w/ express
var port = process.env.PORT || 8080;                // set the port

var bodyParser = require('body-parser');
//var methodOverride = require('method-override')
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

app.use(function (req, res, next) {
    //the code hits this point!
    var data = '';
    req.on('data', function (chunk) {
        data += chunk;
    });
    req.on('end', function () {
        req.rawBody = data;
        next();
    });
    //console.log(data);
});
//app.use(methodOverride());
// routes ======================================================================
require('./routes.js')(app);

// listen (start app with node server.js) ======================================
app.listen(port);
console.log("App listening on port " + port);

routes.js

var _ = require('underscore');

module.exports = function (app) {
    app.post('/', function (req, res) {
        var Validator = require('jsonschema').Validator;
        var v = new Validator();
        var jso = req.rawBody;
        var newjso = JSON.parse(req.rawBody);
        var schema={
            "description": "A payload",
            "type": "object"
        };

        var result = v.validate(newjso,schema);

        console.log('----------------------payload--------------------------------------')
        console.log(newjso);
        console.log('------------------end payload--------------------')

        if (result.errors.length > 0) {
            res.setHeader('content-type', 'application/json');
            res.status(400);
            res.json({
                "error": "Could not decode request: JSON parsing failed"
            });
            console.log('------------------ERROR!!!--------------------')
        }
        else
        {
            var resp = _.filter(_.where(newjso.payload, {drm: true}), function (item) {
                return item.episodeCount > 0
            });

            var newArray = [];
            resp.forEach(function (item) {
                var newItem = _.pick(item, 'image', 'slug', 'title');
                newItem.image = _.propertyOf(newItem.image)('showImage');
                newArray.push(newItem);
            })

            res.setHeader('content-type', 'application/json');
            res.status(200);
            res.json({response: newArray});
        }
    })
}

当我将这个或任何其他 json 发布到邮递员时:

{
  "payload": [
    {
    }]
}

当指定 Contenttypeheader = application/json 时,它看起来像是超时了。为什么它会超时,如何在我的 node.js 程序中解决这个问题?

【问题讨论】:

    标签: javascript node.js postman


    【解决方案1】:

    这是因为bodyParser.json() 已经解析了整个请求,所以end 永远不会在您的自定义中间件中触发。同样,我不明白您为什么要尝试捕获原始请求,而您所做的只是将其传递给 JSON.parse(),而 bodyParser.json() 已经这样做了。

    【讨论】:

    • 好的,如果我使用 bodyparser,如何执行 jsonschema 验证?
    • var newjso = req.body;
    猜你喜欢
    • 2018-01-31
    • 2019-03-16
    • 1970-01-01
    • 2017-12-08
    • 2019-03-26
    • 2017-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多