last day (14 days later) » 

12:08
0
Q: How can I add a static public folder to my node server?

Lee ProbertI have created a new OpenShift node application with their wizard and it has created an index file in the root of the app. I would like to put everything within a public folder and then set this folder as the static. I'm very new to Node and cannot find a solution to this that doesn't break my O...

There is no built in function in Node to serve static files. Your either code it yourself which wouldn't be hard, either use libraries (e.g node-static)
OpenShift uses Express so I think I can use this : app.use(express.static(__dirname + '/public'));
I thought OpenShift is just a cloud hosting
It is yes. I can create an Express web app in node but when I try and port the code over to work on my OpenShift instance I just get a 503 error. I'm not a Node developer so am struggling to understand how to do this.
Any logs from the server?
12:08
Not very helpful:Error: ENOENT, no such file or directory './index.html' at Object.fs.openSync (fs.js:427:18) at Object.fs.readFileSync (fs.js:284:15) at self.populateCache
Do you have index.html in the root folder? This line requires it self.zcache['index.html'] = fs.readFileSync('./index.html');
No. But that's the point: I'm trying to move everything into a folder called 'public' and server it all from there.
Checkout that line I mentioned
Are you sure you need all that openShift code?
hi there
it seems all you errors come of a mess between express and openshift code
12:14
I did try some really simple node code but OpenShift requires some boilerplate so most of the code is what OpenShift gives you when you create a new app
I will try using my old Express code which worked. But I think OpenShift needs the environment variables code
could I just change this to use public/ then?
self.zcache['index.html'] = fs.readFileSync('./index.html');
I am not sure, but it seems half of the openshift code can be removed
ok. I will try that and see about keeping the variable code in.
this stuff :
self.ipaddress = process.env.OPENSHIFT_NODEJS_IP;
self.port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
take a look at the simple static files serving with express stackoverflow.com/a/10444923/2866570
can't help with openshift code though
maybe leave some configurations and edit self.zcache['index.html'] = fs.readFileSync('./index.html'); to something like self.zcache['index.html'] = fs.readFileSync('./public/index.html'); where your static files will be
ok. thanks for your help.
Should I do the same here:
self.routes['/'] = function(req, res) {
res.setHeader('Content-Type', 'text/html');
res.send(self.cache_get('index.html') );
};
self.cache_get('index.html') will get you cached file that was set up in that line we discussed
12:23
cool. thx.

last day (14 days later) »