加载部分在服务器JS上失败 [英] Loading partials fails on the server JS

查看:435
本文介绍了加载部分在服务器JS上失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用MEAN堆栈创建一个简单的单页面应用程序。
到目前为止,我在一个本地主机上工作,一切都很好。上传代码到服务器后,不幸的是,每当我的应用程序试图从我的部分文件夹(HTML模板)中下载任何内容时,我将获得状态代码500(内部服务器错误)。



它不是CORS问题(同一个域),只是为了确保我也安装了CORS插件。



示例路线:

  $ routeProvider.when('/ admin / login',{
templateUrl:'partials / admin / login.html',
controller:'AdminLoginCtrl'
});

我也有路径设置:

  router.get('*',function(request,response){
response.sendfile('./ public / index.html');
}) ;

我搜索了很多页面,我无法找到解决方案。感谢您的帮助。

解决方案

您是否知道您为每个请求发送index.html?



更改此:

  router.get('*',function响应){
response.sendfile('./ public / index.html');
});

为此:

  app.use(express.static(path.join(__ dirname,'public'))); 

或者这个:

  app.use('/ path',express.static(path.join(__ dirname,'public'))); 

如果您在 /



确保将其添加到文件的开头:

  var path = require('path'); 

还要确保你实际上有 public 目录在正确的位置,它包括 index.html 和其他必需的文件。



当然你可能有其他问题,因为你显然没有包括你的整个代码。



如果你想使用Express提供静态文件,请参阅我在GitHub上的示例:





这是一个可以下载的工作示例,将您自己的静态内容放在正确的目录中,并根据自己的需要进行自定义。



更多的例子说明和不使用Express:





其他相关答案:




I am trying to create a simple single page application using MEAN stack. So far I worked on a localhost, and everything worked fine.

Sadly after uploading the code to the server I am getting status code 500 (Internal Server Error) whenever my application try to download anything from my partial folder (HTML templates).

It is not CORS problem (same domain) but just to be sure I also installed CORS plugin.

Example route:

    $routeProvider.when('/admin/login', {
        templateUrl: 'partials/admin/login.html',
        controller: 'AdminLoginCtrl'
    });

I also have the path setting:

router.get('*', function(request, response) {
    response.sendfile('./public/index.html');
});

I've searched through many pages and I am not able to find a solution. Thank you for any help.

解决方案

Do you know that you are sending the index.html for every request?

Change this:

router.get('*', function(request, response) {
    response.sendfile('./public/index.html');
});

To this:

app.use(express.static(path.join(__dirname, 'public')));

Or this:

app.use('/path', express.static(path.join(__dirname, 'public')));

if you went to serve the static files under some path other than /.

Make sure to add this to the beginning of your file:

var path = require('path');

Also make sure that you actually have the public directory in the correct place and that it includes the index.html and other required files.

Of course you may have other problems since you have obviously not included your entire code.

See my example on GitHub if you want to serve static files with Express:

It is a working example that you can download, put your own static content in the correct directory and customize for your own needs.

More examples to do the same with and without Express:

Other related answers:

这篇关于加载部分在服务器JS上失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆