[英]Trouble with $routeProvider in page served by Express.js
I am trying to build my first Express.js/Angular application but am having trouble with $routeProvider
on the page being served by the Express.js application. 我正在尝试构建我的第一个Express.js / Angular应用程序,但是在Express.js应用程序所服务页面上的$routeProvider
遇到了麻烦。 Here is the server.js
: 这是server.js
:
var express = require('express');
var PORT = process.env.PORT || 3000;
var app = express();
app.get('/', function (req, res) {
res.sendFile(__dirname + '/client/views/index.html');
});
app.listen(3000, function () {
console.log("I'm listening");
});
The index.html
page renders and includes the following Angular code: index.html
页面呈现并包含以下Angular代码:
var app = angular.module('app', ['ngRoute']);
app.config(function ($routeProvider) {
$routeProvider
.when('/main', {
templateUrl:'/client/views/main.html'
});
});
Yet when navigating to localhost:3000/#/main
, nothing is injected into the <ng-view>
但是,当导航到localhost:3000/#/main
,没有任何内容注入<ng-view>
Any advice would be greatly appreciated. 任何建议将不胜感激。
Having encountered this issue before I found a solution by telling express to serve files statically. 在让Express静态提供文件之前,我在找到解决方案之前遇到了此问题。
app.use(express.static('client'));
This tells express to send files from the 'client' directory as-is. 这告诉express从“客户端”目录中直接发送文件。 This is used for files such as images, CSS & client side JavaScript. 这用于图像,CSS和客户端JavaScript等文件。 When referencing a file statically, paths are relative to the specified static directory so /client/views/main.html
would be /views/main.html
. 静态引用文件时,路径相对于指定的静态目录,因此/client/views/main.html
将为/views/main.html
。
You can view the documentation for the express static middle ware here . 您可以在此处查看快速静态中间件的文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.