Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...