The easiest way is using app.use().
app.use('/images', express.static(process.env.OPENSHIFT_DATA_DIR+'/images'));
Domanda
I want to start a Nodejs application on Openshift and because I want use persistent files (like images) I need to store them in the OPENSHIFT_DATA_DIR
directory.
I've made this action_hooks for the build phase:
#!/bin/bash
#First of all I check if the dir exists. If not I create it
if [ ! -d $OPENSHIFT_DATA_DIR'images' ]
then
echo "Create "$OPENSHIFT_DATA_DIR'images'
mkdir -p $OPENSHIFT_DATA_DIR'images'
fi
#Create symlink
echo "Create link: "$OPENSHIFT_REPO_DIR'images -> '$OPENSHIFT_DATA_DIR'images'
ln -sf $OPENSHIFT_DATA_DIR'images' $OPENSHIFT_REPO_DIR'images'
The symbolic link is created, but the index.html can't access to the uploaded file
This is my (dumb) index.html
test page:
<html>
<head>
<title>Test page</title>
</head>
<body>
<div id="image_container"/>
<div id="button_container"/>
<script type="text/javascript">
var loaded = false
var debug = false
var clickAction = function() {
if (debug) {
alert(process.env.OPENSHIFT_DATA_DIR)
}
else {
if (!loaded) {
var container = document.getElementById("image_container")
var image = document.createElement("img")
image.src = "images/montagna.jpg"
image.id = "img001"
container.appendChild(image)
loaded = true
}
else {
var container = document.getElementById("image_container")
var image = document.getElementById("img001")
container.removeChild(image)
loaded = false
}
}
}
window.onload = function() {
var container = document.getElementById("button_container")
var button = document.createElement("input")
button.type = "button"
button.name = "button1"
button.value = "clickme"
button.setAttribute("onclick", "clickAction()")
container.appendChild(button)
};
</script>
</body>
</html>
When I click on the button I receive a 404 error from the server (this is the link to the application).
Note that I've uploaded the file via SFTP (with Filezilla) inside the ~/app-root/data/images
directory of the application, and the symlink seems to work because I can see the image also from ~/app-root/repo/images
.
Soluzione
The easiest way is using app.use().
app.use('/images', express.static(process.env.OPENSHIFT_DATA_DIR+'/images'));