zilcuanu
zilcuanu

Reputation: 3715

fetching form data params in nodejs

I am using angularJs to make a http Post request to the server. I am able to get the file uploaded on the NodeJs server, but not able to figure out how to fetch the keywords from the request.

Client Code (AngularJs):

      var file = $scope.myFile;
      var uploadUrl = HOST_URL+"/filter-reports";

      var fd = new FormData();
      fd.append('file', file);
      fd.append('keywords','searchkey1, searchkey2');
      $http.post(uploadUrl, fd, {
            transformRequest: angular.identity,
            headers: {'Content-Type': undefined}
        })
        .success(function(data){
            console.log('Fetched the data .. '+data);
        })
        .error(function(){
            console.log('could not fetch the data .. ');
        });

Please let me know to to extract FormData parameters on Node.js and let me know where am I going wrong as I am new to NodeJs.

Upvotes: 2

Views: 2570

Answers (2)

Naeem Shaikh
Naeem Shaikh

Reputation: 15725

  1. install body-parser module on your node server.

  2. Then on your server, require the module var bodyParser = require('body-parser'); This will parse your request body so you would be access the params inside the body.

  3. in your route for your post request, you can get the keywords parameters as follows,

    var keys=req.body.keywords;

Upvotes: 1

surajck
surajck

Reputation: 1175

req.body.keywords if you are using express with body parser

Upvotes: 1

Related Questions