我刚刚开始探索OpenShift Online 3
,看起来真的棒极了。
我正在尝试设置一个简单的HTTP服务器。
以下是使用CLI
的步骤:
我当前的工作文件夹包含两个文件package.json
和server.js
。
的package.json
{
"name": "test",
"version": "0.0.1",
"description": "Node.js app for OpenShift 3",
"main": "server.js",
"dependencies": {
"express": "^4.13.4"
},
"engine": {
"node": "*",
"npm": "*"
},
"scripts": {
"start": "node server.js"
}
}
Server.js
const express = require('express')
const app = express()
const port = process.env.PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080
const ip = process.env.IP || process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0'
app.get('/', function (req, res) {
res.status(200).send({ message: 'OK' })
})
// error handling
app.use(function(err, req, res, next){
console.error(err.stack)
res.status(500).send('Something bad happened!')
})
app.listen(port, ip)
console.log('Server running on http://%s:%s', ip, port)
module.exports = app
这是控制台输出:
--> Creating resources ...
imagestream "olmeo-openshift" created
buildconfig "olmeo-openshift" created
deploymentconfig "olmeo-openshift" created
service "olmeo-openshift" created
--> Success
Build scheduled, use 'oc logs -f bc/olmeo-openshift' to track its progress.
Run 'oc status' to view your app.
构建完成后,Web控制台会显示错误:
Environment:
DEV_MODE=false
NODE_ENV=production
DEBUG_PORT=5858
Launching via npm...
npm info it worked if it ends with ok
npm info using npm@3.10.9
npm info using node@v6.11.3
npm ERR! Linux 3.10.0-693.12.1.el7.x86_64
npm ERR! argv "/opt/rh/rh-nodejs6/root/usr/bin/node" "/opt/rh/rh-nodejs6/root/usr/bin/npm" "run" "-d" "start"
npm ERR! node v6.11.3
npm ERR! npm v3.10.9
npm ERR! path /opt/app-root/src/package.json
npm ERR! code ENOENT
npm ERR! errno -2
npm ERR! syscall open
npm ERR! enoent ENOENT: no such file or directory, open '/opt/app-root/src/package.json'
npm ERR! enoent ENOENT: no such file or directory, open '/opt/app-root/src/package.json'
npm ERR! enoent This is most likely not a problem with npm itself
npm ERR! enoent and is related to npm not being able to find a file.
npm ERR! enoent
npm ERR! Please include the following file with any support request:
npm ERR! /opt/app-root/src/npm-debug.log
显然,找不到package.json
。我错过了什么?
答案 0 :(得分:6)
如果您使用:
oc new-app .
在远程托管的Git存储库克隆的git工作区中,构建将设置为从托管的Git存储库中提取文件。这意味着如果您进行本地更改并且不将它们添加/提交/推送到托管的Git存储库,那么它们将永远不会被使用。
如果要从本地目录工作而不使用托管Git存储库,则需要使用二进制输入构建。首先要做的是:
oc new-build --binary --image-stream nodejs --name olmeo-openshift
然后通过运行来启动构建:
oc start-build olmeo-openshift --from-dir=.
这将从本地目录上传文件。
构建完第一张图片后,即可运行:
oc new-app olmeo-openshift
部署图像,并且:
oc expose svc/olmeo-openshift
给它一个公共网址。
每次更改代码时,请再次运行:
oc start-build olmeo-openshift --from-dir=.