Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
99 views
in Technique[技术] by (71.8m points)

javascript - Loading partials fails on the server JS

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...