node.js - Angularjs从express.js的 "views"文件夹中路由 "public"文件夹中的ejs静态文件

标签 node.js express gulp mean-stack ejs

这是我的项目结构。 Project-structure

我正在尝试从“public”文件夹中的角度路由文件(app > public > main.js)调用“views”文件夹中的ejs文件(app > views > list.ejs)。但 list.ejs 未加载。

/************main.js****************/
var fsApp=angular.module('chart-app');
fsApp.config(function($routeProvider) {
 $routeProvider
 .when('/', {
	templateUrl: 'list.ejs',
	controller: 'listCtrl'
})

/*In my server.js server file I am calling the files in public folder this way: */
app.use('/static',express.static(__dirname + '/public'));
app.get('/',function(req,res){
  res.render('index.ejs');
});
<!--In index.ejs (where my <ng-view></ng-view> is) I am including the link for static files:-->

<head>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js"></script>
<script src="/static/angular-route.js"></script>
<script src="/static/main.js"></script>
</head>
<body ng-app="chart-app" class="container-fluid">
<div ng-controller="fairshareCtrl">
	<div class="row" ng-view></div>
</div>
</body>

我还需要对服务器文件做些什么吗?我错过了什么?

最佳答案

我不知道你在后端使用什么,但是,当你从express渲染ejs文件时,你必须为其定义引擎。

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

然后你就可以使用

app.get('*',function(req,res){
   res.render('index.ejs');
});

并将所有角边保存在公共(public)文件夹中。

或者您可以只加载 html 文件,而不是通过 ejs 加载 ejs

app.engine('html', require('ejs').renderFile);

app.get('*', (req, res) => {
    res.render('index.html');
})

关于node.js - Angularjs从express.js的 "views"文件夹中路由 "public"文件夹中的ejs静态文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44449889/

相关文章:

javascript - 表单提交在控制台中显示奇怪的错误

node.js - 将 mongoose 结果快速发送到 ajax

node.js - gulp.dest() 忽略子目录

javascript - 尽管在 Controller 中使用数组语法,AngularJS 代码并未正确缩小

npm - gulp-sourcemaps的用途是什么?

node.js - 无法使用 bcrypt 登录用户(dyld : Symbol not found)

node.js - 目标的 Http 代理错误

node.js - mongodb - 按顺序计算缺失索引的最有效方法

javascript - Mongoose 预保存 bcrypt 未保存

node.js - EJS 包含相对于项目根目录的文件