1

Im new with NodeJS and Im trying to send data to server with Angular

$scope.logearse = function () {
        $http({
            method: "POST",
            url: "http://localhost:8888/login",
            data: $scope.usuario
        }).then(function successCallback(response){
                console.log(response)
        }, function errorCallback(error){
            alert("No se han podido enviar los datos")
        })
    } 

But in the server when I try to receipt the request is always {}

http.createServer(function(peticion, respuesta){

    console.log(peticion.url)
    console.log(peticion)
    // We begin with "login"

    if (peticion.url == "/login") {
        console.log("Inside of Login)

        var datosUsuarioLogin = '';

        peticion.addListener("data", function(chunk) {
            datosUsuarioLogin += chunk;

        // function called when a new chunk of data is recibed
        });

        peticion.addListener("end", function() {
            // When the data is recibed is transformed in JSON
            var datosUsuarioLoginObjeto = querystring.parse(datosUsuarioLogin);
            recuperarDatos(datosUsuarioLoginObjeto, respuesta);
            console.log(datosUsuarioLoginObjeto) //return {}
        });
        //End of LOGIN "if"
    }
    }).listen(8888)

The thing is the same code works if I use a form with a regular submit but no if I try to use the $http of ANGULAR.

I try to use "params" instead of "data" but "params" transform the data in the URL and the code dont works.

4
  • Are you setting proper headers along with the request like 'Content-Type': 'application/json' Commented Sep 29, 2016 at 13:21
  • If that's the case,we probably need to confirm what's happening with $scope.usuario. . Commented Sep 29, 2016 at 13:25
  • Those non-English comments in the code aren't helping Commented Sep 29, 2016 at 13:33
  • Comments in English now. $scope.usuario is a Json. And I use 'Content-Type': 'application/json' without succes Commented Sep 29, 2016 at 18:22

2 Answers 2

0

You need to use bodyParser to parse the request body and place the result in request.body of route.

app.use(express.bodyParser());

And the request:

$http({
      method: "POST",
      url: "http://localhost:8888/login",
      contentType: "application/json",
      data: $scope.usuario // if usuario is not a valid json, you could to use JSON.stringify($scope.usuario);
Sign up to request clarification or add additional context in comments.

1 Comment

I still dont use express, yet. I want to learn how to build the app without express (or others addons) and later learn how to use that addons.
0

Ok, after a lot of time trying finnaly I use Express but the version of Express I use dont allow simply bodyParser I need to install the body parser middleware Link to body-parser

And the code

var bodyParser = require('body-parser'); 
var jsonParser = bodyParser.json();

app.post('/', jsonParser, function (request, response) {
response.send(request.body) 
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.