Austin Hunter Austin Hunter - 1 year ago 53
Javascript Question

Sending data from angular to express nodejs server running on different domain

So I am trying to send some data I have in angular to my nodejs server. Let me start with my code:

<body ng-app="starter">
<ion-header-bar class="bar-stable">
<h1 class="title">Ionic Blank Starter</h1>
<div ng-controller="myCtrl">
<input type="text" ng-model="message">
<input type="submit" value="Submit" ng-click="submit()">

Here is a text box with data, then the submit button will send the message and deviceID to the server.


var app = angular.module('starter', []);
app.controller('myCtrl', function($scope, $http) {
var devToken;
var push = new Ionic.Push({
"debug": true
push.register(function(token) {
devToken = token;
console.log("Device token:", token.token);

$scope.submit = function() {

var message = "Hello";
method: 'POST',
url: '',
token: devToken,
message: message


As you can see, I am trying to grab a message and the deviceID and post it to my express server.


var express = require('express');
var bodyParser = require('body-parser');
var apns = require('apns');
var gcm = require('node-gcm');

var app = express();

var sender = new gcm.Sender('AIzaS.........plD2s8');

var registrationTokens = [];
registrationTokens.push('Insert Device ID here that is passed from app.js');

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

app.get('/', function(req, res) {
console.log("DEVICE ID == " + req.body.devToken);
sender.send("Hello", { registrationTokens: registrationTokens });


app.listen(8080, function() {
console.log('running on port 8080');

SO to recap, I am needing help getting my data when submitted to be available on my nodejs express server (which runs at a different domain/port than the amgular app). Can anyone help me figure out how to get my data from angular to express nodejs? Any help is appreciated.


Answer Source

You need to create a route to capture the post:'/', function(req, res) {

Then the front end could pass whatever you want

    $"/", {message:message, devToken:devToken}).success(function(message, status) {
        console.log('Message posted');

If the backend is not on the same origin as the OP states in the comments, you would need to enable cross origin requests.

app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");

Note: don't use * for production unless really want to allow requests from anywhere.