Piter M. Piter M. - 12 days ago 5
AngularJS Question

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.

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

Comments