AngularJS的$ routeProvider templateUrl总是使用Express返回404

Phi*_*p C 5 javascript ejs node.js angularjs

所以问题是AngularJS $routeProvider/ngRoute不能正常工作.我无法让它.html在其路线上显示相应的页面.GET http://localhost:3000/home.html 404 (Not Found)当我尝试在index.ejs页面中加载模板时,我总是得到它.

我已经尝试了很多路径.html来加载,但我还没有成功.我甚home.html至为应用程序中的每个文件夹创建了它,看它是否会抓取任何东西,但没有任何效果.ng-include直接注入html时也不起作用.

/app.js 简化:原始代码使用express.router()

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

app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));

app.use(express.static(path.join(__dirname, 'public')));

app.get('/', function(req,res,next) {
    res.render('index', { page: 'index' });
});

app.listen(3000,function(){ console.log('3k'); });
Run Code Online (Sandbox Code Playgroud)

/views/index.ejs

<!DOCTYPE html>

<html lang="en" ng-app="App">
<head>
    <meta charset="UTF-8">
    <title>WHY???</title>
</head>
<body>

    <div class="main">
        <div ng-view></div>
    </div>

    <script src="/vendor/angular/angular.js"></script>
    <script src="/vendor/angular-route/angular-route.js"></script>
    <script src="/angular/angularApp.js"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

/public/angular/angularApp.js

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

App.config(['$routeProvider', function($routeProvider) {

    $routeProvider
        .when('/', {
            templateUrl: 'views/index/home.html'
        })
        .when('/team', {
            templateUrl: '/views/index/team.html'
        })
        .when('/faq', {
            templateUrl: '/views/index/faq.html'
        })
        .otherwise({
            redirectTo: '/'
        })
}]);
Run Code Online (Sandbox Code Playgroud)

home.html, team.html, and faq.html所有都有简单的代码行用于测试目的.例:<h1> This is home.html </h1>

文件夹结构

app.js

public/
|-vendor/
|-angular/
| |-angularApp.js

views/
|-index.ejs
|
|-index/
| |-home.html
| |-faq.html
| |-team.html
Run Code Online (Sandbox Code Playgroud)

Mos*_*mel 4

Node/Express 静态地提供“public”文件夹中的所有内容。您需要设置相对于公共目录的所有 URL。您的索引文件夹应移至公共目录。

app.js

public/
|-vendor/
|-angular/
| |-angularApp.js
|views/
| |-index/
| | |-home.html
| | |-faq.html
| | |-team.html

views/
|-index.ejs
Run Code Online (Sandbox Code Playgroud)

这个文件夹结构应该可以工作。还要解决这个问题:

    when('/', {
        templateUrl: '/views/index/home.html'
    })
Run Code Online (Sandbox Code Playgroud)