0

When running my openshift app none of the CSS or javescript files are loading.

I have specified the directory in the server.js:

app.use(express.static(__dirname));

In the index.html I have specified the location of the folders:

<link rel="stylesheet" href="style.css" type="text/css" media="screen">
<script src = "controllers/controller.js"></script>

The structure of the folder looks like:

mytest
├── README.md
├── controllers
│   └── controller.js
├── deplist.txt
├── index.html
├── node_modules
├── package.json
├── server.js
└── style.css

When I open the website and open the developer console in chrome I get the following errors:

http://mytest-jamestreasure.rhcloud.com/controller.js 404 Not Found

http://mytest-jamestreasure.rhcloud.com/controllers/controller.js 404 Not Found

I'm not aware of anything else that needs to be added.

Link to the my openshift: http://mytest-jamestreasure.rhcloud.com/

JaAnTr
  • 896
  • 3
  • 16
  • 28

2 Answers2

1

Your construct for defining static content access seems to be correct. Make sure that you are using it with your app context though, e.g.:

myApp.app.use(express.static(__dirname));

Such a problem could be revealed either locally when trying to run the server.js or using rhc tail mytest to check the OpenShift remote logs.


I would also recommend using a dedicated directory for static content that is meant to be used publicly, rather than exposing everything:

app.use(express.static(__dirname + '/public'));
Jiri Fiala
  • 1,400
  • 7
  • 10
0

The issue was because of the following warning, which I was ignoring:

Warning: express.createServer() is deprecated, express
applications no longer inherit from http.Server,
please use:
  var express = require("express");
  var app = express();

It meant I had to change:

self.app = express.createServer();

to:

self.app = app;

This was in the self.initialise = function()

JaAnTr
  • 896
  • 3
  • 16
  • 28
  • You do realize you're no longer experiencing the problem as you have originally posted? You did not include the JS files properly last time, fixed it, and am now answering a completely different question. This isn't remotely an answer to your own question. You're basically saying your incorrectly made function created the 404 errors on your two JS files. – Nelson Oct 20 '15 at 09:46
  • What I have posted did fix my error. I started up my server locally and instead of just getting the warning I posted, which I was ignoring, it threw an error and didn't start up. Fixing this error did make it find my js and css files as I changed nothing else. – JaAnTr Oct 20 '15 at 10:07
  • @JaAnTr, the warning was not causing the problem. You can verify that by reverting your change on `server.js` and trying what I have suggested. It seems you are using the dafault content of the `server.js` file that comes with the node.js cartridge on OpenShift - if so, simply use this `zapp.app.use(express.static(__dirname));` in the main code section (last line in the file), keep the rest unchanged and you should still get the warning, but the files will be accessible as you intended (so e.g. even the server.js will be publicly accessible on your app's URL). – Jiri Fiala Oct 21 '15 at 13:37