I'm following a few tutorials I found online for creating a web app that uses a RESTful API with the MEAN stack.
I'm having trouble implementing both the API server and the Angular routes together. I've created a server.js
file that handles routes to /api/
, so for example I have:
...
app.get('/api', function(req, res) {
res.status(200).json({"message": "Welcome to the app"});
});
So when the API server gets a request it just sends back a JSON message. Now I also have a public folder with a app.js
file that has the Angular code in it:
var app = angular.module('app', ['ngRoute']);
app.config(function($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/index.html',
controller: 'AppController',
resolve: {
message: function(Message) {
return Message.getMessage();
}
}
})
.service("Message", function($http) {
this.getMessage = function() {
return $http.get('/api')
.then(function(response) {
return response;
}, function(response) {
alert("Error retrieving message");
});
}
})
.controller("AppController", function(message, $scope) {
$scope.message = message.data;
});
});
So when I start my server from the command line running node server.js
and go to localhost:5000 (or whatever port it is), I get a Cannot GET /
I'm assuming this is because I don't have a route in my server.js
file to '/'
.
How do I run the app off the app.js file first and have it use the API?