{"id":1238,"date":"2020-11-20T17:21:05","date_gmt":"2020-11-20T09:21:05","guid":{"rendered":"http:\/\/123.57.164.21\/?p=1238"},"modified":"2020-11-20T17:21:05","modified_gmt":"2020-11-20T09:21:05","slug":"cloud-foundry-%e4%b8%ad%e4%bd%bf%e7%94%a8-cfenv","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=1238","title":{"rendered":"Cloud Foundry \u4e2d\u4f7f\u7528 cfenv"},"content":{"rendered":"\n<h5 class=\"wp-block-heading\">1.\u5f53\u6211\u4eec\u5f00\u53d1Nodejs \u540e\u53f0\u4ee3\u7801\uff0c\u5e76\u4e14\u53d1\u5e03\u5230 IBM Cloud Foundry\u65f6\uff0c\u53ef\u80fdNodejs\u540e\u53f0\u4ee3\u7801\u8981\u4f7f\u7528IBM Cloud DB2\u670d\u52a1\uff0c \u5f53\u7136\u6211\u4eec\u53ef\u4ee5\u628aDB2\u8fde\u63a5\u7684URL \u548c \u8ba4\u8bc1\u4fe1\u606f\u7b49\uff0c\u786c\u7f16\u7801\u5728Nodejs\u7a0b\u5e8f\u4e2d\uff0c\u4f46\u662f\u8fd9\u6837\u5b89\u5168\u6027\u4e0d\u9ad8\uff0c\u8fd9\u65f6\u5019\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7cfenv \u6765\u83b7\u5f97IBM Cloud DB2\u7684\u8ba4\u8bc1\u8fde\u63a5\u4fe1\u606f\u3002<\/h5>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>https:\/\/www.npmjs.com\/package\/cfenv<\/p><p>https:\/\/cloud.ibm.com\/docs\/cloud-object-storage?topic=cloud-object-storage-cloud-foundry&#038;locale=zh-CN<\/p><\/blockquote>\n\n\n\n<ul><li>\u9996\u5148\uff0c\u8fdb\u5165\u5230IBM Cloud Foundry\u7684\u63a7\u5236\u53f0\uff0c\u5728\u8fde\u63a5\u7684\u9009\u9879\u4e2d\uff0c\u6dfb\u52a0\u6211\u4eec\u9700\u8981\u7684\u670d\u52a1\u3002\u6bd4\u5982\u8fd9\u91cc\u9762\u6dfb\u52a0cloud-object-storage<\/li><\/ul>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2020\/11\/image-6-1024x255.png\" alt=\"\" class=\"wp-image-1240\" width=\"574\" height=\"143\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2020\/11\/image-6-1024x255.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2020\/11\/image-6-300x75.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2020\/11\/image-6-768x191.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2020\/11\/image-6-1536x383.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2020\/11\/image-6-2048x510.png 2048w, https:\/\/92it.top\/wp-content\/uploads\/2020\/11\/image-6-830x207.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2020\/11\/image-6-230x57.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2020\/11\/image-6-350x87.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2020\/11\/image-6-480x120.png 480w\" sizes=\"(max-width: 574px) 100vw, 574px\" \/><\/figure><\/div>\n\n\n\n<ul><li>\u670d\u52a1\u4fe1\u606f\u5982\u4e0b<\/li><\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">{\n    \"cloud-object-storage\": [\n        {\n            \"credentials\": {\n                \"apikey\": \"abcDEFg_lpQtE23laVRPAbmmBIqKIPmyN4EyJnAnYU9S-\",\n                \"endpoints\": \"https:\/\/control.cloud-object-storage.cloud.ibm.com\/v2\/endpoints\",\n                \"iam_apikey_description\": \"Auto generated apikey during resource-key operation for Instance - crn:v1:bluemix:public:cloud-object-storage:global:a\/123456cabcddda99gd8eff3191340732:7766d05c-b182-2425-4d7e-0e5c123b4567::\",\n                \"iam_apikey_name\": \"auto-generated-apikey-cf4999ce-be10-4712-b489-9876e57a1234\",\n                \"iam_role_crn\": \"crn:v1:bluemix:public:iam::::serviceRole:Manager\",\n                \"iam_serviceid_crn\": \"crn:v1:bluemix:public:iam-identity::a\/ad123ab94a1cca96fd8efe3191340999::serviceid:ServiceId-41e36abc-7171-4545-8b34-983330d55f4d\",\n                \"resource_instance_id\": \"crn:v1:bluemix:public:cloud-object-storage:global:a\/1d524cd94a0dda86fd8eff3191340732:8888c05a-b144-4816-9d7f-1d2b333a1444::\"\n            },\n            \"syslog_drain_url\": null,\n            \"volume_mounts\": [],\n            \"label\": \"cloud-object-storage\",\n            \"provider\": null,\n            \"plan\": \"Lite\",\n            \"name\": \"mycos\",\n            \"tags\": [\n                \"Lite\",\n                \"storage\",\n                \"ibm_release\",\n                \"ibm_created\",\n                \"rc_compatible\",\n                \"ibmcloud-alias\"\n            ]\n        }\n    ]\n}<\/pre>\n\n\n\n<ul><li>\u5728nodejs \u4ee3\u7801\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7appEnv\u6216\u8005\u4e0a\u9762\u7684service\u4fe1\u606f\u3002<\/li><\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">const appEnv = cfenv.getAppEnv();\nconst cosService = 'cloud-object-storage';\n\n\/\/ init the cos sdk\nvar cosCreds = appEnv.services[cosService][0].credentials;\nvar AWS = require('ibm-cos-sdk');\nvar config = {\n    endpoint: 's3.us-south.objectstorage.s3.us-south.cloud-object-storage.appdomain.cloud.net',\n    apiKeyId: cosCreds.apikey,\n    ibmAuthEndpoint: 'https:\/\/iam.cloud.ibm.com\/identity\/token',\n    serviceInstanceId: cosCreds.resource_instance_id,\n};\n\nvar cos = new AWS.S3(config);<\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h5 class=\"wp-block-heading\">2.\u6709\u7684\u65f6\u5019\u6211\u4eec\u9700\u8981\u81ea\u5df1\u5b9a\u4e49\u81ea\u5df1\u7684\u73af\u5883\u53d8\u91cf\uff0c\u7075\u6d3b\u7684\u6765\u5bf9\u5e94\u6d4b\u8bd5\u73af\u5883\u548c\u751f\u4ea7\u73af\u5883\u7684\u53d1\u5e03\u3002\u53ef\u4ee5\u901a\u8fc7 cf env \u547d\u4ee4\u6765\u5b9e\u73b0\u3002<\/h5>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>https:\/\/docs.cloudfoundry.org\/devguide\/deploy-apps\/environment-variable.html<\/p><\/blockquote>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2020\/11\/image-7.png\" alt=\"\" class=\"wp-image-1245\" width=\"461\" height=\"173\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2020\/11\/image-7.png 892w, https:\/\/92it.top\/wp-content\/uploads\/2020\/11\/image-7-300x112.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2020\/11\/image-7-768x288.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2020\/11\/image-7-830x311.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2020\/11\/image-7-230x86.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2020\/11\/image-7-350x131.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2020\/11\/image-7-480x180.png 480w\" sizes=\"(max-width: 461px) 100vw, 461px\" \/><\/figure><\/div>\n\n\n\n<ul><li>\u5728IBM Devops Deliver PipLine\u7684\u90e8\u7f72\u9636\u6bb5<\/li><\/ul>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2020\/11\/image-8-1024x733.png\" alt=\"\" class=\"wp-image-1248\" width=\"484\" height=\"346\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2020\/11\/image-8-1024x733.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2020\/11\/image-8-300x215.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2020\/11\/image-8-768x550.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2020\/11\/image-8-830x594.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2020\/11\/image-8-230x165.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2020\/11\/image-8-350x251.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2020\/11\/image-8-480x344.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2020\/11\/image-8.png 1352w\" sizes=\"(max-width: 484px) 100vw, 484px\" \/><\/figure><\/div>\n\n\n\n<ul><li>\u5728\u90e8\u7f72\u811a\u672c\u4e0a\u4f7f\u7528\u547d\u4ee4 cf set-env WebsocketServer myenv test <\/li><\/ul>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2020\/11\/image-9-1024x866.png\" alt=\"\" class=\"wp-image-1249\" width=\"536\" height=\"453\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2020\/11\/image-9-1024x866.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2020\/11\/image-9-300x254.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2020\/11\/image-9-768x650.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2020\/11\/image-9-830x702.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2020\/11\/image-9-230x195.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2020\/11\/image-9-350x296.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2020\/11\/image-9-480x406.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2020\/11\/image-9.png 1440w\" sizes=\"(max-width: 536px) 100vw, 536px\" \/><\/figure><\/div>\n\n\n\n<ul><li>\u5728\u540e\u53f0\u4ee3\u7801\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7process.env.myenv\u5f97\u5230\u4e4b\u524d\u8bbe\u5b9a\u7684\u503c\u3002<\/li><\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ Start\nserver.on( 'request', app );\nserver.listen( environment.port, function() {\n  \/\/ \u901a\u8fc7process.env.myenv \u5c31\u80fd\u62ff\u5230\u8bbe\u5b9a\u7684\u503c\u3002\n  console.log(process.env.myenv);\n} );<\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h5 class=\"wp-block-heading\">3.Node.js\u4e2d\u73af\u5883\u53d8\u91cfprocess.env\u5230\u5e95\u662f\u4ec0\u4e48\u5462\uff1f<\/h5>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\u6587\u6863\uff1a<a href=\"http:\/\/nodejs.cn\/api\/process.html\">http:\/\/nodejs.cn\/api\/process.html<\/a><\/p><p><a href=\"http:\/\/nodejs.cn\/api\/process.html#process_process_env\">http:\/\/nodejs.cn\/api\/process.html#process_process_env<\/a><\/p><\/blockquote>\n\n\n\n<p>\u5b98\u65b9\u89e3\u91ca\uff1aprocess \u5bf9\u8c61\u662f\u4e00\u4e2a global \uff08\u5168\u5c40\u53d8\u91cf\uff09\uff0c\u63d0\u4f9b\u6709\u5173\u4fe1\u606f\uff0c\u63a7\u5236\u5f53\u524d Node.js \u8fdb\u7a0b\u3002\u4f5c\u4e3a\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5b83\u5bf9\u4e8e Node.js \u5e94\u7528\u7a0b\u5e8f\u59cb\u7ec8\u662f\u53ef\u7528\u7684\uff0c\u6545\u65e0\u9700\u4f7f\u7528 require()\u3002<\/p>\n\n\n\n<p>process\uff08\u8fdb\u7a0b\uff09\u5176\u5b9e\u5c31\u662f\u5b58\u5728nodejs\u4e2d\u7684\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\u3002<\/p>\n\n\n\n<p>\u00a0process.env\u5c5e\u6027\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u7528\u6237\u73af\u5883\u4fe1\u606f\u7684\u5bf9\u8c61\u3002\u539f\u6765\u7740\u4e2a\u5c5e\u6027\u80fd\u8fd4\u56de\u9879\u76ee\u8fd0\u884c\u6240\u5728\u73af\u5883\u7684\u4e00\u4e9b\u4fe1\u606f\u3002\u5f88\u660e\u663e\u7684\u4e00\u4e2a\u4f7f\u7528\u573a\u666f\uff0c\u4f9d\u9760\u8fd9\u4e2a\u6211\u4eec\u5c31\u53ef\u4ee5\u7ed9\u670d\u52a1\u5668\u4e0a\u6253\u4e0a\u4e00\u4e2a\u6807\u7b7e\u3002\u8fd9\u6837\u7684\u8bdd\uff0c\u6211\u4eec\u5c31\u80fd\u6839\u636e\u4e0d\u540c\u7684\u73af\u5883\uff0c\u5728\u4ee3\u7801\u4e0a\u505a\u4e00\u4e9b\u914d\u7f6e\u4e0a\u7684\u5904\u7406\uff0c\u6bd4\u5982dev\uff0cuat\uff0cproduct\u73af\u5883\u7684\u53d1\u5e03\u7684\u95ee\u9898\u3002<\/p>\n\n\n\n<p><strong>\u5982\u4f55\u914d\u7f6e\u73af\u5883\u53d8\u91cf<\/strong><\/p>\n\n\n\n<p>Windows\u914d\u7f6e<\/p>\n\n\n\n<ul><li>\u4e34\u65f6\u914d\u7f6e<\/li><\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\u76f4\u63a5\u5728cmd\u73af\u5883\u914d\u7f6e\u5373\u53ef\uff0c\u67e5\u770b\u73af\u5883\u53d8\u91cf\uff0c\u6dfb\u52a0\u73af\u5883\u53d8\u91cf\uff0c\u5220\u9664\u73af\u5883\u53d8\u91cf\u3002\n\n#node\u4e2d\u5e38\u7528\u7684\u5230\u7684\u73af\u5883\u53d8\u91cf\u662fNODE_ENV\uff0c\u9996\u5148\u67e5\u770b\u662f\u5426\u5b58\u5728\nset NODE_ENV\n\n#\u5982\u679c\u4e0d\u5b58\u5728\u5219\u6dfb\u52a0\u73af\u5883\u53d8\u91cf\nset NODE_ENV=production\n\n#\u73af\u5883\u53d8\u91cf\u8ffd\u52a0\u503c set \u53d8\u91cf\u540d=%\u53d8\u91cf\u540d%;\u53d8\u91cf\u5185\u5bb9\nset path=%path%;C:\\web;C:\\Tools\n\n#\u67d0\u4e9b\u65f6\u5019\u9700\u8981\u5220\u9664\u73af\u5883\u53d8\u91cf\nset NODE_ENV=<\/pre>\n\n\n\n<ul><li>\u6c38\u4e45\u914d\u7f6e<\/li><\/ul>\n\n\n\n<p>\u53f3\u952e(\u6b64\u7535\u8111) -> \u5c5e\u6027(R) -> \u9ad8\u7ea7\u7cfb\u7edf\u8bbe\u7f6e -> \u73af\u5883\u53d8\u91cf(N)&#8230;<\/p>\n\n\n\n<p>Linux\u914d\u7f6e<\/p>\n\n\n\n<ul><li>\u4e34\u65f6\u914d\u7f6e<\/li><\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#node\u4e2d\u5e38\u7528\u7684\u5230\u7684\u73af\u5883\u53d8\u91cf\u662fNODE_ENV\uff0c\u9996\u5148\u67e5\u770b\u662f\u5426\u5b58\u5728\necho $NODE_ENV\n\n#\u5982\u679c\u4e0d\u5b58\u5728\u5219\u6dfb\u52a0\u73af\u5883\u53d8\u91cf\nexport NODE_ENV=production\n\n#\u73af\u5883\u53d8\u91cf\u8ffd\u52a0\u503c\nexport path=$path:\/home\/download:\/usr\/local\/\n\n#\u67d0\u4e9b\u65f6\u5019\u9700\u8981\u5220\u9664\u73af\u5883\u53d8\u91cf\nunset NODE_ENV\n\n#\u67d0\u4e9b\u65f6\u5019\u9700\u8981\u663e\u793a\u6240\u6709\u7684\u73af\u5883\u53d8\u91cf\nenv<\/pre>\n\n\n\n<ul><li>\u6c38\u4e45\u914d\u7f6e<\/li><\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\u6253\u5f00\u914d\u7f6e\u6587\u4ef6\u6240\u5728\n# \u6240\u6709\u7528\u6237\u90fd\u751f\u6548\nvim \/etc\/profile\n\n# \u5f53\u524d\u7528\u6237\u751f\u6548\nvim ~\/.bash_profile\n\n\u5728\u6587\u4ef6\u672b\u5c3e\u6dfb\u52a0\u7c7b\u4f3c\u5982\u4e0b\u8bed\u53e5\u8fdb\u884c\u73af\u5883\u53d8\u91cf\u7684\u8bbe\u7f6e\u6216\u4fee\u6539\n# \u5728\u6587\u4ef6\u672b\u5c3e\u6dfb\u52a0\u5982\u4e0b\u683c\u5f0f\u7684\u73af\u5883\u53d8\u91cf\nexport path=$path:\/home\/download:\/usr\/local\/\nexport NODE_ENV = product\n\n\u6700\u540e\u4fee\u6539\u5b8c\u6210\u540e\u9700\u8981\u8fd0\u884c\u5982\u4e0b\u8bed\u53e5\u4ee4\u7cfb\u7edf\u91cd\u65b0\u52a0\u8f7d\n# \u4fee\u6539\/etc\/profile\u6587\u4ef6\u540e\nsource \/etc\/profile\n\n# \u4fee\u6539~\/.bash_profile\u6587\u4ef6\u540e\nsource ~\/.bash_profile<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1.\u5f53\u6211\u4eec\u5f00\u53d1Nodejs \u540e\u53f0\u4ee3\u7801\uff0c\u5e76\u4e14\u53d1\u5e03\u5230 IBM Cloud Foundry\u65f6\uff0c\u53ef\u80fdNodejs\u540e\u53f0\u4ee3 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,10,11],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/1238"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1238"}],"version-history":[{"count":13,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/1238\/revisions"}],"predecessor-version":[{"id":1255,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/1238\/revisions\/1255"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}