Tuesday 6 June 2017

confused with nodejs and angular

i read many articles and posts about integrating nodeJS & angular, i don't understand at all. i tried to create the server.js, mainController and index.html, the server runs well but angular is not working. i've been stuck with this for one week and i can't solve it.

here is my server.js :

var express = require('express'),
app = express(),
port = process.env.PORT || 3000;
var routes = require('./routes/routes');

app.use(express.static(__dirname + '/application')); //static path
routes(app);

app.get('/', function(req,res){
    res.sendFile(__dirname + "/application/views/index.html")
    //res.sendFile('./views/index.html');
});

app.listen(port);
console.log('server started on: ' + port);

here is the mainController.js :

var app = angular.module('server',[]);

// App controller
app.controller('appController', ['$scope','dataServ', function($scope, Data) {

    $scope.greetings = "hello words";

    Data.get()
        .success(function(resp) {
            $scope.greetings = resp;
        });
}]);

and this is my index.html :

<!DOCTYPE html>
<html lang="en" ng-app="mainController">
<head>
    <meta charset="UTF-8">
    <title>Testing Web</title>

    <!-- LOAD JS -->
    <script src="../lib/js/angular.min.js"></script>
    <script src="../controllers/mainController.js"></script>

</head>

<body>
    <div ng-controller="appController">
        
    </div>
</body>

</html>

here is my folder structure if needed :

  • application
    -- controllers
    --- mainController.js
    -- lib
    --- js
    ---- angular.min.js
    -- views
    --- index.html
  • routes
  • server.js


via rinaldy31

No comments:

Post a Comment