2017-04-22 39 views
2

嗯,我剛開始編程我被困在一個地方。如何獲得在node.js中的身體參數在node.js中

我讓我像

response: --------------------------e2a4456320b2131c 


sent 
--------------------------e2a4456320b2131c 
Content-Disposition: form-data; name="is_test" 

true 
--------------------------e2a4456320b2131c 
Content-Disposition: form-data; name="success" 

true 
--------------------------e2a4456320b2131c-- 

請求參數我如何可以獲取這一切PARAMS:

is_test成功

這裏是我的代碼:

var express = require('express'), 
bodyParser = require('body-parser'); 

var app = express(); 

app.use(bodyParser.json()); 

exports.helloWorld = functions.https.onRequest((request, response) => { 
                var body = ""; 

            request.on('data', function (data) { 
                  body += data; 
                 }); 
            request.on('end', function() {                
                  console.log('response: ' + body); 




                 });            

    }); 
+0

我知道,我需要使用bodyParser但我該如何使用。這就是我不知道 –

+1

'app.use(bodyParser.json());'好了,這不是JSON,所以......你試過其他的選擇嗎?原始的,文本或網址編碼? –

回答

1

您需要配置路由器來處理請求。看一下世界各地的快遞公司的Hello World示例http://expressjs.com/en/starter/hello-world.html

此外,如果您要在正文中發送數據,您正在尋找http post方法。 例子:

const bodyParser = require('body-parser'); 
const express = require('express'); 
const app = express(); 

app.use(bodyParser.json()); 

app.post('/', function (req, res) { 
    const body = req.body; 
    /*Body logic here*/ 
    res.status(200).end(); 
}) 

app.listen(3000, function() { 
    console.log('Example app listening on port 3000!'); 
}) 
+0

但功能還沒有出現app.post(在這裏).. –

+0

我不明白你的意思。您發佈的代碼不完整,您可以擴展它以顯示整個應用程序初始化嗎? – catacs

+0

請:https://gist.github.com/SunnyShah407/5eec68b3fe283453bfccf5b62ecea1a2 –