1

I have a webpage that I have hosted using a node application on openshift. Its here http://nodejs-volition.rhcloud.com/

My question is very simple (although I haven't found anyone else asking it). How do I refer to other files in the directory which contains index.html

For instance I would like to use an image that is in the directory in the index. My current html for the image is

<img src="$OPENSHIFT_REPO_DIR/images/1416870991752.jpg" alt="spark core">

I have also tried using "images/1416870991752.jpg". I have the same problem with linking to other html files in the directory? What am I doing wrong? Please help?

Arthur
  • 31
  • 5
  • possible duplicate of [How to serve an image using nodejs](http://stackoverflow.com/questions/5823722/how-to-serve-an-image-using-nodejs) –  Dec 02 '14 at 12:47
  • Hi, thanks for your comment. I don't believe that my question is a duplicate, but I will try to use the answer there as much as possible and come back to this one. Thanks again. – Arthur Dec 02 '14 at 13:45

1 Answers1

1

As corey112358 alludes to below the key is in that to host using nodejs a server must be defined. My application already has a server file, so rather than creating a new server I must modify the existing one. I've done it successfully now, there were two changes to make to the server.js file.

The 1st change is modification of the cache. That should look like this...

self.zcache['index.html'] = fs.readFileSync('./index.html');
self.zcache['page2.html'] = fs.readFileSync('./page2.html');
self.zcache['sparkcoredark.jpg'] = fs.readFileSync('./sparkcoredark.jpg');

The first line was already included but the next two were added by me to include another html page and an image.

The second step is modify the self.createRoutes section of the server.js file as below (asciimo image is included by default).

self.createRoutes = function() { self.routes = { };

    self.routes['/asciimo'] = function(req, res) {
        var link = "http://i.imgur.com/kmbjB.png";
        res.send("<html><body><img src='" + link + "'></body></html>");
    };

    self.routes['/'] = function(req, res) {
        res.setHeader('Content-Type', 'text/html');
        res.send(self.cache_get('index.html') );
    };
    self.routes['/page2.html'] = function(req, res) {
        res.setHeader('Content-Type', 'text/html');
        res.send(self.cache_get('page2.html') );
    };
    self.routes['/sparkcoredark.jpg'] = function(req, res) {

       res.setHeader('Content-Type', 'image/jpg');
        res.send(self.cache_get('sparkcoredark.jpg') );

    };
};

Hope that helps out anyone else struggling with this issue. Thanks to coreyfibonacci

Arthur
  • 31
  • 5