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

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

问题描述

我正在尝试使用 MEAN 堆栈创建一个简单的单页应用程序.到目前为止,我在本地主机上工作,一切正常.

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

遗憾的是,在将代码上传到服务器后,每当我的应用程序尝试从部分文件夹(HTML 模板)下载任何内容时,我都会收到状态代码 500(内部服务器错误).

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).

这不是 CORS 问题(同一域),只是为了确保我还安装了 CORS 插件.

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

示例路线:

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

我也有路径设置:

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.

推荐答案

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

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

改变这个:

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

为此:

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

或者这个:

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');

还要确保 public 目录确实位于正确的位置,并且它包含 index.html 和其他必需的文件.

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.

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

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.

更多使用 Express 和不使用 Express 执行相同操作的示例:

More examples to do the same with and without Express:

其他相关答案:

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

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