user3557777 user3557777 - 1 year ago 101
AngularJS Question

How to send data from angularjs controller to server(nodejs) controller in meanjs

Am trying to send my data from angular form to the server controller where
I use those data to modify my json file, it seems in meanjs they use ngResource
to communicate backend and FrontEnd data but I failed to comprehend how it works
Below are the parts of my code which I judged may help to explain my problem.


<section data-ng-controller=’MachineController’>
<form class=’form-horizontal’ data-ng-submit=’create()’ novalidate>
<div class="form-group">
<label class="control-label" for="projectName">Name of the project</label>
<div class="controls">
<input type="text" class="form-control" id="projectName" data-ng-model="projectName" placeholder="my_first_project">
<div class="form-group">
<input type="submit" class="btn btn-primary"></input>


(function() {
'use strict';
//Setting up route

routeConfig.$inject = ['$stateProvider'];

function routeConfig($stateProvider) {
// Machine state routing
.state('machine', {
url: '/machine',
templateUrl: 'modules/machine/client/views/machine.client.view.html',
controller: 'MachineController',
controllerAs: 'vm'


(function() {
'use strict';

.controller('MachineController', MachineController);

MachineController.$inject = ['$scope'];

function MachineController($scope) {
var vm = this;

// Machine controller logic
$scope.create = function() {
console.log("Testing the functionalites");

function init() {}


'use strict';
* Module dependencies.
var path = require('path'),
mongoose = require('mongoose'),
initialJsonFile = require('../resources/config.json'),
finalJsonFile = './modules/machine/server/config/config1.json',
updatedJson = require('../config/config1.json'),
js2xmlparser = require('js2xmlparser'),
jsonfile = require('jsonfile'),
errorHandler = require(path.resolve('./modules/core/server/controllers/errors.server.controller')),
_ = require('lodash');
* Converting xml to json
exports.updatingJsonConfig = function(req, res) {

//I do need here to get data from angular form the string 'testing description' it was only used to test the modifications
initialJsonFile.project.projectName = 'testing description';



'use strict';
var machine = require('../controllers/machine.server.controller');
module.exports = function(app) {

NB : I used one input in the form to only explain the problem,but it is a form of multiple fields

Answer Source

In the MEAN stack you use Express on the server side (Node) to set up a HTTP server, and your Angular front-end application communicates with the server through the HTTP protocol. It's not included in your posted code, but I assume there is a http server set up in your server side code. something like

var server = http.createServer(app);

On your local computer, you will be able to access the http server set up like this on localhost:8080.


This line defines a route handler, in this context it means "when you hit localhost:8080/testing with a HTTP GET REQUEST execute machine.updatingJsonConfig.

updatingJsonConfig is an express middleware function, that has access to the req and res objects. If you want to respond to the HTTP request you need to call res.send() at the end of your updatingJsonConfig function with whatever response you want to send as an argument.

Then on the client side you need to make a http request to your server and process the response. The simplest way of doing this is using the $http service like:

.then(function(response) {
 // do something with the response

ngResource makes the same HTTP request, you don't really need it here.

So res.send() on the server, $http.get in the client.


In order to send form data through HTTP you need to make a POST request with $ instead of $http.get. GET requests don't have a body.

On the server side you need to change your route to handle POST requests


You'll also need to include the body-parser module in express, and you'll have a req.body available.