简体   繁体   English

带有Angular的Node.js / Express-404错误

[英]Nodejs/Express with Angular - 404 error

I've been trying to learn the MEAN stack and wanted to do an application to test it out. 我一直在尝试学习MEAN堆栈,并想编写一个应用程序对其进行测试。 The aim was to have a static variable (in this case 'hello') displayed by Angular in the HTML, I can't seem to get my $scope variable in my controller to display, having looked in the developer console on my browser I saw 'GET 127.0.0.1:3000/app.js 404 not found'. 目的是让Angular在HTML中显示静态变量(在本例中为“ hello”),在浏览器的开发者控制台中查看后,我似乎无法在控制器中显示$ scope变量看到'找不到127.0.0.1:3000/app.js 404'。

This is my node server using express: 这是我使用express的节点服务器:

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

app.get('/', function(req, res){

        res.sendFile(__dirname + '/views/index.html');
});



app.listen(3000, function() {
        console.log('I\'m listening on port 3000...');
});

This is my index.html file (a directory below in /views): 这是我的index.html文件(/ views下面的目录):

 <!DOCTYPE html>
<html ng-app="myModule">
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/
 angular.min.js"></script>
    <script type="text/javascript" src="app.js"></script>
</head>

<body>
<h1>Sucess!</h1>

        <div ng-controller="myController">
                <p>Angular test: {{ message }}</p>
        </div>
</body>

And this is my app.js file containing my module and controller (also in /views): 这是我的app.js文件,其中包含我的模块和控制器(也在/ views中):

var myApp = angular.module('myModule', []);

    myApp.controller('myController', ['$scope',
function($scope) {

    $scope.message = 'hello';

}]);

I'd be very grateful if anyone could help out! 如果有人可以帮忙,我将不胜感激!

You will have to serve all the files, for example using express.static , not just index.html : 您将必须提供所有文件,例如使用express.static ,而不仅仅是index.html

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

app.use(express.static('views'));

app.listen(3000, function() {
    console.log('I\'m listening on port 3000...');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM