public > main.js)调用“views”文件夹中的ejs文件(app > views > list.ej-6ren">
gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 22:50:56 26 4
gpt4 key购买 nike

这是我的项目结构。 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com