javascript Angular js 路由,500 内部服务器错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24218898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular js Routes, 500 internal server error
提问by Harshit Laddha
I have a directory structure like this -
我有一个这样的目录结构 -
-project
-public
-app
-app.js ( angular app module )
-server
-server.js ( node root js file )
-includes
-layout.jade
-scripts.jade
-views
-index.jade
-partials
-main
-main.jade
-about.jade
-about-vid.jade
footer_main.jade
-account
-login.jade
Now when i try and use partials all in one 'partials'directory everything works fine, but when i try to organize them in seperate directories i get internal server error . here is the normal relevant code - app.js -
现在,当我尝试在一个“部分”目录中使用所有部分时,一切正常,但是当我尝试将它们组织在单独的目录中时,出现内部服务器错误。这是正常的相关代码 - app.js -
angular.module('app',['ngResource','ngRoute']);
angular.module('app').config(function($routeProvider,$locationProvider){
$locationProvider.html5Mode(true);
$routeProvider
.when('/', { templateUrl: '/partials/main', controller: 'mainCtrl'});
});
server.js code -
server.js 代码 -
app.use(express.static(__dirname + '/public'));
app.get('/partials/:partialPath',function(req,res){
res.render('partials/' + req.params.partialPath);
});
app.get('*',function(req,res){
console.log(mongoMessage);
res.render('index');
});
index.jade rendered by server.js -
server.js 渲染的 index.jade -
include ../includes/layout
.header-white.navbar.navbar-default.navbar-static-top
.container
.navbar-header
button(data-target=".navbar-collapse",data-toggle="collapse",class="navbar-toggle btn navbar-btn")
span.icon-bar
span.icon-bar
span.icon-bar
a(href="/",class="navbar-brand")
img(alt="CampuStop",id="logoimg",src="/img/logo_big.png",style="height:74px")
div(ng-include="'/partials/login'")
block main-content
div(ng-view)
div(ng-include="'/partials/footer_main'")
include ../includes/scripts
But if i try to organize them and make the following changes i get 500 internal server error in console for my login, app.js - change template url to /partials/main/main/
但是,如果我尝试组织它们并进行以下更改,我会在控制台中收到 500 内部服务器错误以供我登录,app.js - 将模板 url 更改为/partials/main/main/
angular.module('app').config(function($routeProvider,$locationProvider){
$locationProvider.html5Mode(true);
$routeProvider
.when('/', { templateUrl: '/partials/main/main', controller: 'mainCtrl'});
});
index.jade - change the include urls to respective directories :
index.jade - 将包含 url 更改为相应目录:
div(ng-include="'/partials/account/login'")
div(ng-include="'/partials/main/footer_main'")
server.js - replace :partialParams by *
server.js -将 :partialParams 替换为 *
app.get('/partials/*',function(req,res){
res.render('partials/' + req.params);
});
Can any one tell me where i am going wrong or what other information should i provide?
谁能告诉我哪里出错了或者我应该提供哪些其他信息?
采纳答案by Harshit Laddha
I had to change the
我不得不改变
app.get('/partials/*',function(req,res){
res.render('partials/' + req.params);
});
to -
到 -
app.get('/partials/*',function(req,res){
res.render('partials/' + req.params['0']);
});
as when i printed the req.params in console i found it returning a object instead of the path which actually created the routing and rendering the partials incorrectly
当我在控制台中打印 req.params 时,我发现它返回一个对象而不是实际创建路由并错误渲染部分的路径
回答by cyberwombat
When Angular requests a template it sets the base path relative to the location of the tempalte file. Consequently any calls to partials are now being called from a different directory relative to the parent template. You will want to adjust your partial paths to account for this.
当 Angular 请求模板时,它会设置相对于模板文件位置的基本路径。因此,现在从相对于父模板的不同目录调用对部分的任何调用。您将需要调整部分路径以解决此问题。
Perhaps something like:
也许是这样的:
div(ng-include="'../partials/account/login'")
You are getting the 500 error because Angular cannot find the partial - it's probably trying to get main/partials/account/login
您收到 500 错误,因为 Angular 找不到部分 - 它可能正在尝试获取 main/partials/account/login