Sunny Shah
Sunny Shah

Reputation: 13020

How to get body parameters in node.js

Well I had just started programming in node.js. I am stuck at one place.

I am getting my request parameter like

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


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

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

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

How can i fetch all this params :

is_test , success and etc.

Here is my code :

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);
                                            });
                        );

Upvotes: 5

Views: 9031

Answers (1)

catacs
catacs

Reputation: 311

You need to configure the router to handle the request. Take a look at the hello world example of express docs http://expressjs.com/en/starter/hello-world.html

Moreover if you are sending data in the body you are looking for a http post method. Example:

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!');
})

Upvotes: 6

Related Questions