2017-01-03 8 views
1

NodeJS APIのPOSTmanによるPOST要求では、空のボディーを受け取ります...自動テストは完全に動作します。実際に私はそれを "フォーム"または "テキスト"の "生"として送信するときに起こるPostmanからですが、生のJSONとして送信すると、単に読み込み中にフリーズします。
PostmanからNodeJSのAPIへのPOST要求の空のボディ(自動テストからではありません)

app.use(bodyParser.urlencoded({extended: true})); 
app.use(bodyParser.json()); 

全体のコードはここにある:私はそれはそれはポストマンのためのテストのために働くではなく、作られたパース体に関連したこれらの2行を追加することについて読んhttps://github.com/nemenosfe/TakeMe-Api しかし、3つのキーファイルは(簡体字)、次のとおりです。

app.js

const express = require('express'); 
const bodyParser = require('body-parser'); 
const app = express(); 
const cors = require('cors'); 
const user = require('./routes/users'); 

app.use(bodyParser.urlencoded({extended: true})); 
app.use(bodyParser.json()); 

app.use('/users', user); 

app.use(cors()); 

// catch 404 and forward to error handler 
app.use(function(req, res, next) { 
    var err = new Error('Not Found'); 
    err.status = 404; 
    next(err); 
}); 

// development error handler 
// will print stacktrace 
if (app.get('env') === 'development') { 
    app.use(function(err, req, res, next) { 
    res.status(err.status || 500); 
    }); 
} 

// production error handler 
// no stacktraces leaked to user 
app.use(function(err, req, res, next) { 
    res.status(err.status || 500); 
}); 

app.listen(8888, function() { 
    console.log("Node server running on http://localhost:8888"); 
}); 

module.exports = app; 

ルート/ユーザー

"use strict" 
const express = require('express'), 
     router = express.Router(), 
     /* ... many require and other code probably not relevant for the problem ... */ 

router 
    .post('/', function(req, res, next) { 
    console.log(`conditions: ${(!req.body)} ${(!req.body.uid)} ${(!req.body.provider)} ${JSON.stringify(req.body)}`); 
    // This console.log appears as follows in the console once I make the request by Postman: false true true {} 
    // But it receives what it shoulds with automated tests 

    if (!req.body || !req.body.uid || !req.body.provider) { utilsErrors.handleNoParams(res); } 
     else { 
     /* ... There is a lot of code here but it's not relevant for the problem because it doesn't even reaches this point. */ 
    } 
    }) 

module.exports = router 

テスト/ユーザー

"use strict" 
let request = require('supertest-as-promised'); 
const api = require('../app'); 
/* ... Another require not relevant for the problem ... */ 
request = request(api); 

describe('Users route', function() { 
    describe.only('POST /users', function() { 
    it("should create a new user when it doesn't exist", function(done) { 
     const params = { 
     'appkey': helperCommon.appkey, 
     'uid': 1, 
     'provider': 'providerTest', 
     'name': 'fakeName' 
     }; 
     request 
     .post('/users') 
     .set('Accept', 'application/json') 
     .send(params) 
     .expect(201) 
     .expect('Content-Type', /application\/json/) 
     .then((res) => { 
      expect(res.body).to.have.property('user'); 
      const userResponse = res.body.user; 
      expect(userResponse).to.have.property('uid', params.uid); 
      expect(userResponse).to.have.property('provider', params.provider); 
      expect(userResponse).to.have.property('name', params.name); 
      /* ... other expectectations that are not important for the problem ... */ 
      done(); 
     }, done) 
    }); 
    }); 

ありがとう!

答えて

1

POSTリクエストを送信していることを確認してください。x-www-form-urlenconded

関連する問題