2016-02-29 60 views
9


我正在試圖向我的節點JS服務器發出接受post/put調用的請求。我嘗試通過chai發送後調用的參數在服務器(req.body.myparam)上不可見。
我曾嘗試用下面POST請求,但最終有沒有效果: -
通過Chai的發佈請求

var host = "http://localhost:3000"; 
var path = "/myPath"; 
chai.request(host).post(path).field('myparam' , 'test').end(function(error, response, body) { 

chai.request(host).post(path).send({'myparam' : 'test'}).end(function(error, response, body) { 

節點JS代碼如下: -

app.put ('/mypath', function(req, res){      //Handling post request to create league 
    createDoc (req, res); 
}) 


app.post ('/mypath', function(req, res){      //Handling post request to create league 
    createDoc (req, res); 
}) 

var createDoc = function (req, res) { 
    var myparam = req.body.myparam;         //league id to create new league 
    if (!myparam) { 
     res.status(400).json({error : 'myparam is missing'}); 
     return; 
    }  
}; 

上面代碼中去到myparam丟失。

請讓我知道什麼是做同樣的最好的方式。
在此先感謝。

+0

你可以分享端點的代碼嗎? –

+0

更新了代碼。如果你需要其他東西,請告訴我。 –

+0

我沒有看到在任何地方定義的「聯盟」? – Derek

回答

13

你寫的方式,我假設你用的是chai-http包。 .field()功能在chai-http中不起作用。另一位用戶指出here並在github上打開了一個問題。

這裏是你怎麼可以這樣寫:

.set('content-type', 'application/x-www-form-urlencoded') 
.send({myparam: 'test'}) 

這裏是成功將參數傳遞給服務器的完整代碼:

test.js

'use strict'; 
var chai = require('chai'); 
var chaiHttp = require('chai-http'); 

chai.use(chaiHttp); 

describe('Test group', function() { 
    var host = "http://" + process.env.IP + ':' + process.env.PORT; 
    var path = "/myPath"; 

    it('should send parameters to : /path POST', function(done) { 
     chai 
      .request(host) 
      .post(path) 
      // .field('myparam' , 'test') 
      .set('content-type', 'application/x-www-form-urlencoded') 
      .send({myparam: 'test'}) 
      .end(function(error, response, body) { 
       if (error) { 
        done(error); 
       } else { 
        done(); 
       } 
      }); 
    }); 
}); 

server.js

'use strict'; 
var bodyParser = require("body-parser"), 
    express  = require("express"), 
    app   = express(); 

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

app.put ('/mypath', function(req, res){ //Handling post request to create league 
    createDoc (req, res); 
}); 

app.post ('/mypath', function(req, res){ //Handling post request to create league 
    createDoc (req, res); 
}); 

var createDoc = function (req, res) { 
    console.log(req.body); 
    var myparam = req.body.myparam; //league id to create new league 
    if (!myparam) { 
     res.status(400).json({error : 'myparam is missing'}); 
     return; 
    } 
}; 

app.listen(process.env.PORT, process.env.IP, function(){ 
    console.log("SERVER IS RUNNING"); 
}); 

module.exports = app; 
+0

這對我有效。這很有趣,因爲文檔在發佈表單數據時會說使用.field()方法,但如果您在服務器上以這種方式使用bodyParser,那麼這可能不起作用? –

+0

如何在這附上一個文件?我知道這是代碼.attach('imageField',fs.readFileSync('avatar.png'),'avatar.png')但我不知道在哪附上它 – Kannan

3

我找到了兩種方法解決這個問題,用空的req.body

  1. body作爲一種形式的數據

    .put('/path/endpoint') 
    .type('form') 
    .send({foo: 'bar'}) 
    // .field('foo' , 'bar') 
    .end(function(err, res) {} 
    
    // headers received, set by the plugin apparently 
    'accept-encoding': 'gzip, deflate', 
    'user-agent': 'node-superagent/2.3.0', 
    'content-type': 'application/x-www-form-urlencoded', 
    'content-length': '127', 
    
  2. body作爲application/json

    .put('/path/endpoint') 
    .set('content-type', 'application/json') 
    .send({foo: 'bar'}) 
    // .field('foo' , 'bar') 
    .end(function(err, res) {} 
    
    // headers received, set by the plugin apparently 
    'accept-encoding': 'gzip, deflate', 
    'user-agent': 'node-superagent/2.3.0', 
    'content-type': 'application/json', 
    'content-length': '105', 
    

在兩種情況下我使用.send({foo: 'bar'})和不.field('foo' , 'bar')

這個問題顯然與chai-http無關。這是superagent的問題。並且chai-http正在使用superagent

superagent試圖玩機器學習和猜測我們。以下是他們的docs say

默認情況下發送的字符串將設置Content-Typeapplication/x-www-form-urlencoded

SuperAgent的格式是可擴展的,但是默認情況下「JSON」和「形式」的支持。發送數據爲application/x-www-form-urlencoded只需使用「表單」調用.type(),默認爲「json」。

request.post('/user') 
    .type('form') 
    .send({ name: 'tj' }) 
    .send({ pet: 'tobi' }) 
    .end(callback) 

chai-http最大的錯誤是,他們沒有正確地記錄他們的插件。你必須通過互聯網搜索答案,而不是在chai-http GitHub頁面上。

+0

如何在這個文件中附加文件?我知道這是代碼.attach('imageField',fs.readFileSync('avatar.png'),'avatar.png')但我不確定在哪裏附加它 – Kannan

相關問題