2

I want to setup a node.js backend to serve an application I am building. I am using openshift and the setup went fine, and I had a my basic index.html showing up fine. However, when I tried to add my css, and js files, I could not.

<link rel="stylesheet" type="text/css" href="css/style.css">

I found this page, and I am having the same issue as described, with no solution yet.

https://www.openshift.com/forums/openshift/problem-linking-css-and-js-in-my-nodejs-app

Iman Askur
  • 302
  • 1
  • 12

2 Answers2

1

I got this working by adding this code in my server.js

server.use('/static', express.static(__dirname + '/resources'));

where i saved all my static files inside resources folder.

Saravanan S
  • 1,021
  • 1
  • 10
  • 24
  • It should be noted that this line should be inserted inside the "self.initializeServer = function()" – svarog Jul 11 '14 at 05:48
1

Solved! I have my CSS and JS folders with appropriate files in them, and the server.js code now looks like this to initialize the server:

 /**
 *  Initialize the server (express) and create the routes and register
 *  the handlers.
 */
self.initializeServer = function() {
    self.createRoutes();
    self.app = express.createServer();


    self.app.configure(function(){
                       //self.app.use(express.cookieParser());
                       //self.app.use.(express.session({secret:"secret",key:"express.sid"}));
                       ['css', 'img', 'js', 'plugin', 'lib'].forEach(function (dir){
                                                                     self.app.use('/'+dir, express.static(__dirname+'/'+dir));
                                                                     });
                       self.app.set('views', __dirname + '/views');
                       self.app.set('view engine', 'ejs');
                       });


    //  Add handlers for the app (from the routes).
    for (var r in self.routes) {
        self.app.get(r, self.routes[r]);
    }
};
Iman Askur
  • 302
  • 1
  • 12