0

I am trying to build a simple "Hello world" Node.js/AngularJS app and am struggling. I am running the app through localhost and struggling to figure out why the HTML page isn't finding my script files.

server.js

var express = require('express');
var fs = require('fs');
var app = express();

app.set('port', (process.env.PORT || 5000));

app.use(express.static(__dirname + '/public'));
app.set('views', __dirname + '/app');
app.set('view engine', 'ejs');

app.get('/', function(request, response) {
  response.render('index');
});

app.listen(app.get('port'), function() {
  console.log('Node app is running on port', app.get('port'));
});

The server is being run successfully. The angularJS application is listed below:

index.ejs

<!DOCTYPE html>

<html>

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-route.js"></script>
    <script src="app.js"></script>
    <script src="Controllers/home.ctrl.js"></script>

</head>

<body ng-app>

    <div ng-view></div>

</body>

</html>

app.js

angular.module("app", ["ngRoute"])
    .config(function($routeProvider){
        $routeProvider
            .when("/", {
                controller: "HomeController",
                templateUrl: "app/Views/home.html"
            });
    });

home.html home.ctrl.js

Folder structure:

  • Web server
    • index.js
    • app
      • Controllers
      • Views
      • app.js
      • index.ejs

The error I am getting in the console:

HTTP404: NOT FOUND - The server has not found anything matching the requested URI (Uniform Resource Identifier).
GET - http://localhost:5000/app.js
HTTP404: NOT FOUND - The server has not found anything matching the requested URI (Uniform Resource Identifier).
GET - http://localhost:5000/Controllers/home.ctrl.js
1
  • in the route config in the controller you should pass the string name of the controller Commented Feb 21, 2016 at 20:48

1 Answer 1

1

The problem is here:

<script src="app.js"></script>
<script src="Controllers/home.ctrl.js"></script>
<script src="app/app.js"></script>

You include two app.js, one of them has the wrong path. Maybe you meant index.js instead.

UPDATE

I see another problem here:

app.use(express.static(__dirname + '/public'));

You serve the folder public, but those files are in app

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Tamas, I am aware that this is not correct but that does not solve the issue. I was testing to see if it was the directory that was wrong and I was mistaken - but forgot to remove from the code before posting the question. I have updated it. Thanks though

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.