Ionic HTTP POST - 500 Internal error
<p>I use HTTP Post for post to my Heroku Server. When I put variable direct in server like this (I use Postman) :</p>

<pre><code>var user = "Mélanie",
name = "Sortie au musée",
tag = "servertest";
</code></pre>

<p>It's work, but when I use (with click on ionic button) :</p>

<pre><code>var user = req.body.user,
name = req.body.name,
tag = req.body.tag;
</code></pre>

<p>I have Internal error 500 ...</p>

<p>My controller :</p>

<pre><code>var SERVER_SIDE_URL = "https://myapp.herokuapp.com";
var pushUrl = SERVER_SIDE_URL + "/cancelpush";

$scope.canceled = function() {
    $http.post(pushUrl, {
        params: {
            user: "Mélanie",
            name: "Sortie à la salle",
            tag: "servertest"
        }
    }).success(function(data) {
        console.log("Success bro !" + data)
    }).error(function(data) {
        alert("ERROR" + data);
    });
};
</code></pre>

<p>My server :</p>

<pre><code>app.post('/cancelpush', function(req, res) {
  var user = req.body.user,
    name = req.body.name,
    tag = req.body.tag;

  var sendNotification = function(data) {
    var headers = {
      "Content-Type": "application/json",
      "Authorization": "Basic MyKey(Secret)"
    };

    var options = {
      host: "onesignal.com",
      port: 443,
      path: "/api/v1/notifications",
      method: "POST",
      headers: headers
    };

    var https = require('https');
    var req = https.request(options, function(res) {
      res.on('data', function(data) {
        console.log("Response:");
        console.log(JSON.parse(data));
      });
    });

    req.on('error', function(e) {
      console.log("ERROR:");
      console.log(e);
    });

    req.write(JSON.stringify(data));
    req.end();
  };

  var message = {
    app_id: "myappidwithnumbers(Secret)",
    contents: {
      "en": user + " not come " + name + " ! why ... "
    },
    tags: [{
      "key": tag,
      "relation": "=&gt;",
      "value": tag
    }]
  };

  sendNotification(message);
});
</code></pre>

<p>Stack trace :</p>

<pre><code>Request URL:https://myapp.herokuapp.com/cancelpush

Request Method:POST

Status Code:500 Internal Server Error
Remote Address:54.197.245.254:443

Response Headers

Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:X-Requested-With,content-type
Access-Control-Allow-Methods:GET, POST, OPTIONS, PUT, PATCH, DELETE
Access-Control-Allow-Origin:*
Connection:keep-alive
Content-Length:21
Content-Type:text/html
Date:Wed, 16 Mar 2016 19:54:18 GMT
Server:Cowboy
Via:1.1 vegur
X-Powered-By:Express

Request Headers

Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate
Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Content-Length:66
Content-Type:application/json;charset=UTF-8
Host:mysterious-castle-64979.herokuapp.com
Origin:http://127.0.0.1:8103
Referer:http://127.0.0.1:8103/
User-Agent:Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B137 Safari/601.1

Request Payload

{user: "Mélanie", name: "Sortie à la salle", tag: "servertest"}
name: "Sortie à la salle"
tag: "servertest"
user: "Mélanie"
</code></pre>

<p>Please help me ... Thanks !</p>

<p>EDIT (Question solved, thanks to hallucinations) :</p>

<p>The error come because user is not defined :</p>

<pre><code>2016-03-16T18:10:58.833465+00:00 app[web.1]: TypeError: Cannot read property 'user' of undefined 
2016-03-16T18:10:58.833503+00:00 app[web.1]: at Object.handle (/app/server.js:132:22) 
2016-03-16T18:10:58.833508+00:00 app[web.1]: at next_layer (/app/node_modules/express/lib/router/route.js:103:13) 
2016-03-16T18:10:58.833509+00:00 app[web.1]: at Route.dispatch (/app/node_modules/express/lib/router/route.js:107:5) 
2016-03-16T18:10:58.833510+00:00 app[web.1]: at /app/node_modules/express/lib/router/index.js:195:24 
2016-03-16T18:10:58.833511+00:00 app[web.1]: at Function.proto.process_params (/app/node_modules/express/lib/router/index.js:251:12) 
2016-03-16T18:10:58.833512+00:00 app[web.1]: at next (/app/node_modules/express/lib/router/index.js:189:19) 
2016-03-16T18:10:58.833512+00:00 app[web.1]: at Layer.handle (/app/server.js:50:5) 
2016-03-16T18:10:58.833513+00:00 app[web.1]: at trim_prefix (/app/node_modules/express/lib/router/index.js:226:17) 
2016-03-16T18:10:58.833514+00:00 app[web.1]: at /app/node_modules/express/lib/router/index.js:198:9 
2016-03-16T18:10:58.833514+00:00 app[web.1]: at Function.proto.process_params (/app/node_modules/express/lib/router/index.js:251:12)
</code></pre>

<p>With this Server JS, it's work :</p>

<pre><code>// init instances

var qs = require('querystring');
var request = require('request');
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var https = require('https');

// var stripe     = require("stripe")(STRIPE_API_SECRET_KEY);

var app = express();
var router = express.Router();
var port = process.env.PORT || 9311;

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: false
}));

// Cross Domain Origin Setup
var allowCrossDomain = function(req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
    res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');

    // intercept OPTIONS method
    if ('OPTIONS' == req.method) {
        res.send(200);
    } else {
        next();
    }
};
app.use(allowCrossDomain);
app.use(function(req, res, next) {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
    res.setHeader('Access-Control-Allow-Credentials', true);
    next();
});

router.get('/', function(req, res) {
    res.json({
        message: 'hola ! welcome to our triple problem last api!'
    });
});


var url = 'http://requestb.in/wf21lpwf'
request(url, function(error, response, body) {
    if (!error) {
        console.log(body);
    }
});

app.post('/cancelpush', function(req, res) {
    // console.log(req);
    // console.log(res);

    var user = req.body.user;
    var name = req.body.name;
    var tag = req.body.tag;


    console.log(require('util').inspect(req.body.user));

    // var user = "Mélanie",
    //   name = "Sortie au musée",
    //   tag = "servertest";

    var sendNotification = function(data) {
        var headers = {
            "Content-Type": "application/json",
            "Authorization": "Basic MmNkNjc0YtYTcwODZmN2Y4MTYz"
        };

        var options = {
            host: "onesignal.com",
            port: 443,
            path: "/api/v1/notifications",
            method: "POST",
            headers: headers
        };

        var https = require('https');
        var req = https.request(options, function(res) {
            res.on('data', function(data) {
                console.log("Response:");
                console.log(JSON.parse(data));
            });
        });

        req.on('error', function(e) {
            console.log("ERROR:");
            console.log(e);
        });

        req.write(JSON.stringify(data));
        req.end();
    };

    var message = {
        app_id: "2549a5d6-e4d3afd941d9",
        contents: {
            "en": user + " notification " + name + " ! "
        },
        tags: [{
            "key": tag,
            "relation": "=&gt;",
            "value": tag
        }]
    };

    console.log(require('util').inspect(message));

    sendNotification(message);

});


app.use(bodyParser.urlencoded({
    extended: true
}));
app.use(bodyParser());
app.use(bodyParser.json());
app.use('/api', router); // register our route
app.listen(port);

console.log('Magic happens on port ' + port);
</code></pre>

<p>I hope it's can help someone, thanks to Hallucinations !</p>