jeudi 16 juin 2016

invoke node.js server-side service in angularjs. getting following error. Error: [$injector:nomod] Module ‘angularjsNodejsTutorial’ is not available

This is the error: Error: [$injector:modulerr] Failed to instantiate module angularjsNodejsTutorial due to: Error: [$injector:nomod] Module ‘angularjsNodejsTutorial’ is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.” exception. from browser or postman when I hit localhost:3000/dirPath then I get the data back but not through this html file.

//here are the files: index.html, app.js(angularjs) and index.js(node)

//index.html

<!DOCTYPE html>
    <html ng-app="angularjsNodejsTutorial">
    <head>
        <title>Integrating AngularJS with NodeJS</title>
        <script src="http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.2/angular.js"></script>
        <script src="../node_modules/angular-route/angular-route.min.js"></script>
    </head>
    <body >
    <div ng-controller="myController">

        <ul >
            <li> The Files Are: {{data}}</li>
        </ul>
    </div>

    <script src="../public/javascripts/app.js" type="text/javascript"></script>
    <script src="../node_modules/angular-route/angular-route.js"></script>
    </body>

    </html>

//(AngularJS Client-Side)app.js

var app = angular.module('angularjsNodejsTutorial',['ngRoute']);
app.controller('myController', function($scope, $http) {
    $scope.data = [];
    var request = $http.get('/dirPath');
    request.success(function(data) {
        console.print("The files from this directory are:", data);

        $scope.data = data;
    });
    request.error(function(data){
        console.log('Error: ' + data);
    });
});

//Server-side node.js index.js file

var express = require('express');
var router = express.Router();
var path = require('path');

/* GET home page. */
router.get('/', function(req, res, next) {
  res.sendFile(path.join(__dirname, '../', 'views', 'index.html'));
});

router.get("/dirPath", function(req, res) {
  var fs = require("fs");

  var dir = '/Users/swapnil/Documents/Test';

  fileList = [];
  var files = fs.readdirSync(dir);
  for(var i in files){
    if (!files.hasOwnProperty(i)) continue;
    var name = dir+'/'+files[i];
    if (!fs.statSync(name).isDirectory()){
      fileList.push(name);
    }
  }
  return res.send(fileList);

});

module.exports = router;

Aucun commentaire:

Enregistrer un commentaire