redhat 旗下站点,提供多种语言的免费云空间。
- 免费注册用户
- 点选右上角“MY APPS”
- “Create Application”
- 找到“Node.js 0.6”(有同学问:我不要0.6,我要最新版!!!请接着看...)
- 填写“Public URL” --> “Create Application”
- 恭喜你,得到一个1G容量的个人网站,开始你的Nodejs创作之旅吧
$ git clone ssh://[email protected]/~/git/exp.git/
即可把项目成功下载到本地,修改你的code,利用git命令去提交你的code,立即可以看到最新效果!
$ ssh [email protected]
即可远程登录你的空间,相关指令可以用 help 命令查看,也可看这里
编辑server.js,在self.createRoutes内新增:
self.routes['/env'] = function(req, res) {
res.setHeader('Content-Type', 'text/html');
res.send('<!DOCTYPE html><html>\n' +
'<head><title>Node.js Process Env</title></head>\n' +
'<body>\n<br />\n' +
'Node Version: ' + process.version +
'\n<br />\nEnv: {<br />\n<pre>' +
(function(content) {
for (k in process.env) {
content += ' ' + k + ': ' + process.env[k] + '\n';
}
return content;
})('') + '}\n</pre><br/>\n' +
'</body>\n</html>');
};
$ git add server.js
$ git commit -m 'create a route to show process environment.'
$ git push
好了,现在可以访问你的站点,http://yours-app.rhcloud.com/env,即可检查Nodejs运行环境
$ touch .openshift/markers/NODEJS_VERSION
$ echo '0.10.15' >> .openshift/markers/NODEJS_VERSION
$ mkdir .openshift/lib
$ vim .openshift/lib/utils
复制如下代码到你的文件
#!/bin/bash
function get_node_version() {
marker="$OPENSHIFT_REPO_DIR/.openshift/markers/NODEJS_VERSION"
nodejs_ver=$(egrep -v "^\s*#.*" "$marker" | egrep -v "^\s*$" | tail -1)
echo "${nodejs_ver:-$marker}"
}
function get_node_install_dir() {
echo "$OPENSHIFT_DATA_DIR"
}
function get_npm_bin_path() {
ver=${1:-"$(get_node_version)"}
echo "$(get_node_install_dir)/node-v$ver-linux-x64/bin"
}
function get_node_bin_path() {
echo "$(get_npm_bin_path $@)"
}
function get_node_tmp_dir() {
ztmpdir="$OPENSHIFT_DATA_DIR/.nodejs.tmp"
[ -d "$ztmpdir" ] || mkdir -p "$ztmpdir"
echo "$ztmpdir"
}
function _install_nodejs() {
ver=${1:-"$(get_node_version)"}
zfile="node-v${ver}-linux-x64.tar.gz"
zlink="http://nodejs.org/dist/v${ver}/${zfile}"
instdir="$(get_node_install_dir)"
dldir="$OPENSHIFT_DATA_DIR/downloads"
mkdir -p "$dldir"
echo " - Downloading and extracting $zlink ... "
if ! curl -L -o "$dldir/$zfile" "$zlink"; then
echo " - ERROR -- download failed for $zlink"
echo " - download uri = $dldir/$zfile"
return 1
fi
(cd "$instdir"; tar -zxf "$dldir/$zfile")
echo " - Done installing Node.js version $ver"
}
function _ensure_bash_profile_setup() {
dot_bash_profile=$OPENSHIFT_DATA_DIR/.bash_profile
pattern='\s*source(.*)\.openshift/lib/setup_custom_nodejs_env\s*(.*)\s*'
if ! egrep "$pattern" $dot_bash_profile > /dev/null 2>&1 ; then
cat >> $dot_bash_profile <<SRCEOF
source "\$OPENSHIFT_REPO_DIR/.openshift/lib/setup_custom_nodejs_env"
SRCEOF
echo " - Added source setup_custom_nodejs_env to .bash_profile"
fi
}
function ensure_node_is_installed() {
ver=${1:-"$(get_node_version)"}
echo " - Checking to see if Node.js version $ver is installed ... "
if [ -d "$(get_node_bin_path)" ]; then
echo " - Node.js version $ver is already installed"
else
_install_nodejs "$ver"
fi
_ensure_bash_profile_setup
}
function setup_path_for_custom_node_version() {
ver=${1:-"$(get_node_version)"}
node_bin_path=$(get_node_bin_path "$ver")
export PATH="$node_bin_path:${PATH}"
if [ -n "$_SHOW_SETUP_PATH_MESSAGES" ]; then
echo " - PATH set to include custom node version ($ver) from"
echo " $node_bin_path "
echo " PATH = $PATH"
fi
}
保存
$ vim .openshift/lib/setup_custom_nodejs_env
复制下面的内容
# bash profile ($OPENSHIFT_DATA_DIR/.bash_profile).
source $OPENSHIFT_REPO_DIR/.openshift/lib/utils
function _setup_path_and_remove_wrappers() {
[ -z "$ZDEBUG" ] || echo "Setting path to include custom Node version"
setup_path_for_custom_node_version
unset node
unset npm
unset _setup_path_and_remove_wrappers
}
function npm() {
_setup_path_and_remove_wrappers
npm "$@"
}
function node() {
_setup_path_and_remove_wrappers
node "$@"
}
保存
$ vim .openshift/action_hooks/pre_build
复制下面的内容
#!/bin/bash
source "$OPENSHIFT_REPO_DIR/.openshift/lib/utils"
echo ""
ensure_node_is_installed
mv "${OPENSHIFT_REPO_DIR}/package.json" "$(get_node_tmp_dir)"
保存
$ vim .openshift/action_hooks/build
复制下面的内容
#!/bin/bash
source "$OPENSHIFT_REPO_DIR/.openshift/lib/utils"
_SHOW_SETUP_PATH_MESSAGES="true" setup_path_for_custom_node_version
tmp_package_json="$(get_node_tmp_dir)/package.json"
if [ -f "$tmp_package_json" ]; then
[ -f "${OPENSHIFT_REPO_DIR}/package.json" ] || \
mv "$tmp_package_json" "${OPENSHIFT_REPO_DIR}/package.json"
fi
if [ -f "${OPENSHIFT_REPO_DIR}"/package.json ]; then
echo " - Installing dependencies w/ new version of npm ... "
echo
(cd "${OPENSHIFT_REPO_DIR}"; export TMPDIR="/tmp"; npm install -d)
fi
保存
# vim .openshift/action_hooks/pre_start_nodejs-0.6
复制下面内容
#!/bin/bash
source "$OPENSHIFT_REPO_DIR/.openshift/lib/utils"
ver=$(get_node_version)
echo ""
echo " - pre_start_nodejs: Adding Node.js version $ver binaries to path"
_SHOW_SETUP_PATH_MESSAGES="true" setup_path_for_custom_node_version
保存
$ git add .openshift
$ git commit -m 'create shell bash to upgrade nodejs version'
$ git push
再次访问你的站点,http://yours-app.rhcloud.com/env,检查Nodejs运行环境 结束。