mercredi 6 mai 2015

AngularJS, NodeJs and ExpressJS

The program is just to do a sum to learning how to works angular, express and nodejs.

My library is:

angular.js angular-route.js

I am trying run this code but I am geeting that error:

Uncaught TypeError: url.match is not a function (13:04:31:527 | error,  javascript)
  at completeRequest (public_html/js/libs/http://ift.tt/1Eh6Fq6)
  at xhr.onreadystatechange              (public_html/js/libs/http://ift.tt/1RdoW1J)

My HTML is:

<head>
    <title>TODO supply a title</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <script src="js/libs/http://ift.tt/1Eh6Fqd" type="text/javascript"></script>
    <script src="js/libs/http://ift.tt/1RdoWhX" type="text/javascript"></script>
    <script src="script.js" type="text/javascript"></script>

</head>
<body ng-controller="AppCtrl" >

    <form >
        <input type="text" ng-model="url" size="80" aria-label="URL" />
        <br>
        <br>            Parameter 1: 
        <input type="text" ng-model="param1">
        <br>
        <br>
        Parameter 2: 
        <input type="text" ng-model="param2">
    </form>
    <br>
    <br>
     <button id="samplegetbtn" ng-click="click('http://localhost:8000/calc/param1/:param1/param2/:param2')">Sample GET</button>
     <br>
     <div >Sum: {{data}}</div>
    <br>
</body>

My Server side is:

var url = require('url');
//Require express, 
var express = require('express');
//and create an app
var app = express();

//app.get() which represents the HTTP GET method
app.get('/', function (req, res) {

  res.send('Hello World!');

});

//app.get() which represents the HTTP GET method
app.get('http://localhost:8000/calc/param1/:param1/param2/:param2', function (req, res) {

var a = parseInt(req.params.param1);
  console.log(a);
  var b = parseInt(req.params.param2);
  console.log(b);


var output = a + b;
  console.log(output);
  res.sendStatus(output);

});



var server = app.listen(8000, function () {

  var host = server.address().address;
  var port = server.address().port;

  console.log('Example app listening at http://' + host + ':' + port);

});

My script with AngularJS:

(function(angular) {
'use strict';
 angular.module('myCalc', ['ngRoute'])



 .controller('AppCtrl', function($scope, $http, $templateCache) {
    $scope.click = function(url) {
    $scope.url = url;
    var param1 = $scope.param1;
  var param2 = $scope.param2;

$http.get({ url: $scope.url, param1: $scope.param1, param2: $scope.param2, cache: $templateCache}).
 //    $http.get({ url: $scope.url,  cache: $templateCache}).
        success(function(data) {
        $scope.data = data;
       }).
        error(function(data) {
        $scope.data = data || "Request failed";
      });

  };

})


 .config(function($routeProvider, $locationProvider) {
   $routeProvider
   .when('http://localhost:8000/calc/param1/:param1/param2/:param2', {
    templateUrl: 'index.html',
    controller: 'AppCtrl'


   // configure html5 to get links working on jsfiddle
   $locationProvider.html5Mode(true);
 });
})(window.angular);

Aucun commentaire:

Enregistrer un commentaire