AngularJS $ http.get не работает с маршрутами express.js - PullRequest
0 голосов
/ 18 октября 2018

Я работаю с приложением, использующим AngularJS, Node.js, Express.js и MongoDB.

AngularJS Part:

index.html:

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

    app.config(['$routeProvider','$locationProvider', 
        function($routeProvider,$locationProvider){
            $routeProvider
                .when('/', {
                    templateUrl : 'show.html',
                    controller  : 'EmployeeController'
                }).when('/nothing', {
                    templateUrl : 'main.html',
                    controller  : 'mainController'
                });
            $locationProvider.html5Mode(true);
    }]);

    app.controller('EmployeeController',function($scope,$http) {
        $scope.getEmployees = function() {
            console.log("working");

        $http.get("http:localhost:8080/employees").then(function(response) {
                console.log(response.data);
            });
        }
    });

Часть Node.js:

server.js:

app.use(express.static(__dirname + '/angularjs/public'));
app.use(cors());
app.disable('etag');

app.use('/',function(req, res) {
    res.sendFile(__dirname + '/angularjs/public/index.html');
});

require('./app/routes')(app);

app.listen(8080);

rout.js:

var employee = require('./controller/EmployeeController');
module.exports = function(app) {
    app.route('/employees').get(employee.showEmployees);
};

EmployeeController.js:

exports.showEmployees = function(req, res) {
  employeeModel.find({}, function(err, data) {
    if (err)
      res.send(err);
     else
      res.send(data);
  });
};

Экспресс-маршрутизация работает хорошо.Получение данных из MongoDB.Проблема с AngularJS.

Когда я запускаю URL «localhost: 8080 / employee», он возвращает всю страницу «index.html» как ответ на $ http.get в AngularJS.

Может ли кто-нибудь помочь мне, где я не прав?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...