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

AngularJS's $routeProvider templateUrl is always returning 404 using Express

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

我尝试了多种路径来加载 .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'); });

/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>

/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: '/'
        })
}]);

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

Node/Express 正在为 "public" 文件夹中的所有内容提供静态服务。您需要设置所有相对于 public directory.Your 索引文件夹的 URL 应移至 public.

app.js

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

views/
|-index.ejs

此文件夹结构应该有效。 同时解决这个问题:

    when('/', {
        templateUrl: '/views/index/home.html'
    })