usersam
usersam

Reputation: 1245

node.js express is not receiving request

I am using express web framework and trying to make an $http request from angularjs. I am passing data to request from client but server is not receiving request for some unknown reasons. Please help.

server.js

var express = require('express');
var app = express();
var http = require('http');
var server = http.createServer(app);
var io = require('socket.io')(server);

var path = require('path');
var fs = require('fs');

app.use(express.static(path.join(__dirname, 'public')));

app.post('/readTfile',function (req,res) {
    console.log('i received a request');
    console.log(req.body);  
});

server.listen(3000);

And angular html

<html>   
   <head>
      <title>File tream 2</title>
        <script type="text/javascript" src="javascripts/angular.js"></script>                   
   </head>

   <body>
      <h2>File tream 2 AngularJS</h2>
        <div ng-app = "mainApp">            
            <div id="readfile" ng-controller = "Ctrl1">                 
                <div>{{myfiledata}}</div> </br></br>
            </div>   
       </div>    
   </body>
 <script> 
var mainApp = angular.module("mainApp",[])

mainApp.controller('Ctrl1', function ($scope, $http) {
        var filename = 'D:\\myapp\\public\\test.txt';       
        var obj = {"filename" : filename};
        $scope.myfiledata = 'result';
        $http({
                url: '/readTfile',
                method: "POST",
                data: JSON.stringify(obj),
                //timeout: canceller.promise,
                headers: {'Content-Type': 'application/json','charset' : 'utf-8'}
                }).success(function(result) {
                console.log(result);
                $scope.myfiledata = 'result';                       
                }).error(function(data, status) {
                console.log(data);
                }); 
});            
</script> 
</html>

On console i am getting undefined for req.body

i received a request

undefined

Please help to solve me this problem.

Upvotes: 3

Views: 5636

Answers (1)

jfriend00
jfriend00

Reputation: 708116

You will need middleware to read the body of the POST request from the incoming stream and to parse it from JSON to a Javascript object and put that into req.body. It does not just end up in req.body automatically. The usual middleware for a simple JSON body would be to use the body-parser middleware that is built into Express.

// other stuff here

// read and parse application/json
app.use(express.json());

app.post('/readTfile',function (req,res) {
    console.log('i received a request');
    console.log(req.body);  
    res.send("something");
});

And, for this middleware to work and automatically recognize that you sent JSON, you will have to make sure the post has set the right content type.

Note, there is different middleware for different content types. The code above is for application/json. If you are doing a vanilla form post, that would typically have a content-type of application/x-www-form-urlencoded and you would use:

app.use(express.urlencoded());

The middleware shown here will automatically detect which content-type is available only operate on a content-type that matches their functionality so you can even have both of these middleware present.

Upvotes: 10

Related Questions