2017-04-22 92 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 –