bharath
bharath

Reputation: 885

unable to read the data of incoming request in node js

I am trying to create a sample login page using node,express and angularjs.

Below is my login view

<div class="loginpage">
<div class="loginpage_content">
    <div style="margin-top:30px;padding:10px;width:100%;text-align:center;font-size:20px;font-weight:bold">Please Enter Your Login Crendtials</div>
    <table width="80%" style="margin-top:40px;margin-left:50px;text-align:center;font-size:20px">
        <tr height="40px"><td width="25%">UserName</td><td width="5%">:</td><td width="70%"><input type="text" ng-model="name"></td></tr>
        <tr height="20px"></tr>                     
        <tr height="40px"><td width="25%">Password</td><td width="5%">:</td><td width="70%"><input type="password" ng-model="pass"></td></tr>
        <tr height="30px"></tr> 
        <tr height="40px"><td  colspan="3" style="align:left"><input type="button" value="Login" ng-click="login()"><input type="button" value="Clear" style="margin-left:10px"><a href="" style="margin-left:10px">Change Password</a></td></tr>   
    </table>
    <div style="margin-top:10px;padding:10px;width:99%;text-align:center;font-size:20px;Color:red;display:none">Enter valid Username & Password</div>   
</div>

controller for login page

angular.module("Fms").controller('LoginCtrl',['$scope','$http',function($scope,$http)
{
$scope.results ="";
$scope.name ="";
$scope.pass ="";
$scope.login=function()
{
    $http(
    {   
        method: 'get',
        url: '/login',
        data:  'LOG CHECK',
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    }).
    success(function(response) 
    {
        console.log("success"); // Getting Success Response in Callback
        console.log(response);
    }).
    error(function(response) 
    {

    });
}
  }]);

code for serving incoming request

  server.get('/login', function(req, res)
  {
         console.log(req.data);
        res.send("received");
  });

problem is i want to read the data passed via get request. I tried many things...

console.log(req.data); returns undefined why????

Upvotes: 0

Views: 336

Answers (1)

Mritunjay
Mritunjay

Reputation: 25882

There are two problems in your code.

1) Angularjs $http get takes field as params to send the data with request. And params value should be an object.

So send request as bellow

$http(
{   
    method: 'get',
    url: '/login',
    params:  {data:'LOG CHECK'},
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
})

2) On server side read get data like req.query.data instead of req.data.

Upvotes: 1

Related Questions