aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/learn
diff options
context:
space:
mode:
authorFlorian Merz <me@fiji-flo.de>2021-03-08 22:37:08 +0100
committerFlorian Merz <me@fiji-flo.de>2021-03-08 22:37:08 +0100
commit59f1389c6023be8ec1435f8f7e55d7de5a302b5b (patch)
tree07bb735b7c0e522627185912020e74bdccd2bd38 /files/zh-cn/learn
parente3d7a4ae7d79c409bbe2db23c9b0acc2f01718d4 (diff)
downloadtranslated-content-59f1389c6023be8ec1435f8f7e55d7de5a302b5b.tar.gz
translated-content-59f1389c6023be8ec1435f8f7e55d7de5a302b5b.tar.bz2
translated-content-59f1389c6023be8ec1435f8f7e55d7de5a302b5b.zip
sync translated content
Diffstat (limited to 'files/zh-cn/learn')
-rw-r--r--files/zh-cn/learn/server-side/express_nodejs/installing_on_pws_cloud_foundry/index.html242
1 files changed, 0 insertions, 242 deletions
diff --git a/files/zh-cn/learn/server-side/express_nodejs/installing_on_pws_cloud_foundry/index.html b/files/zh-cn/learn/server-side/express_nodejs/installing_on_pws_cloud_foundry/index.html
deleted file mode 100644
index e3f321e83a..0000000000
--- a/files/zh-cn/learn/server-side/express_nodejs/installing_on_pws_cloud_foundry/index.html
+++ /dev/null
@@ -1,242 +0,0 @@
----
-title: 在 PWS/Cloud Foundry 上,安装 LocalLibrary
-slug: learn/Server-side/Express_Nodejs/Installing_on_PWS_Cloud_Foundry
-translation_of: Learn/Server-side/Express_Nodejs/Installing_on_PWS_Cloud_Foundry
----
-<div>{{LearnSidebar}}</div>
-
-<p class="summary">本文提供了如何在 <a href="http://run.pivotal.io">Pivotal Web Services PaaS cloud </a>云上安装 LocalLibrary的实际演示 - 这是 Heroku 的全功能,开源替代品,Heroku 是我们<a href="/zh-CN/docs/Learn/Server-side/Express_Nodejs/deployment">教程第 7 部分</a>中使用的 PaaS 云服务。如果您正在寻找 Heroku(或其他 PaaS 云服务)的替代方案,或者只是想尝试不同的东西,PWS / Cloud Foundry 绝对值得一试。</p>
-
-<h2 id="为什么是_PWS">为什么是 PWS?</h2>
-
-<p>Pivotal Web Services 是<a href="https://www.cloudfoundry.org/">开源云代工平台</a>的公共实例。它是一个多语言平台,支持许多不同的语言,包括Node.js,Java,PHP,Python,Staticfiles 和 Ruby。它有一个免费试用版,对于运行 Node 应用程序非常有效!由于 Node 和 Express 是开源项目,因此与使用 Cloud Foundry 等开放式部署平台保持一致。您可以<a href="https://github.com/cloudfoundry">深入了解</a>应用程序的托管方式。</p>
-
-<p>使用PWS有多种原因!</p>
-
-<ul>
- <li>PWS具有<a href="https://run.pivotla.io/pricing">灵活的定价</a>,可以很好地适应 node 等小型运行。你可以用每月不到 5 美元的价格,运行一对冗余的应用程序。这包括备用故障转移系统,如果主服务器在任何时候出现故障,它将接管运行您的应用程序。</li>
- <li>作为PaaS,PWS为我们提供了大量的 Web 基础设施。这使得入门更加容易,因为您不必担心服务器、负载平衡器、反向代理、崩溃时重新启动网站,或者PWS 为我们提供的任何其他 Web 基础结构。</li>
- <li>因为 PWS 使用的是 Cloud Foundry,一个开放的平台。因此,您可以轻松地将应用程序,部署到其他 Cloud Foundry 提供商,例如 <a href="https://www.ibm.com/cloud-computing/bluemix/">IBM BlueMix</a>, <a href="https://www.anynines.com/">AnyNines</a> 和 <a href="https://www.swisscom.ch/en/business/enterprise/offer/cloud-data-center-services/paas/application-cloud.html">Swisscomm Application Cloud</a>。以下说明,适用于任何标准的 Cloud Foundry 部署,只需稍加修改即可。</li>
- <li>虽然它确实有一些限制,但这些不会影响这个特定的应用程序。例如:
- <ul>
- <li>PWS 和 Cloud Foundry 仅提供短期存储,因此用户上载的文件,无法安全地长期存储在 PWS 本身上。</li>
- <li>免费试用一年有效,限制最高 87美元的应用程序使用量。对于典型的 Node 应用程序,这意味着您可以运行一整年的应用程序。</li>
- </ul>
- </li>
- <li>大多数情况下它只是可以工作,如果你最终喜欢它,并希望升级,那么扩展你的应用程序非常容易。</li>
- <li>PWS 和其他 Cloud Foundry 应用程序,可用于生产级别的应用程序。</li>
-</ul>
-
-<h2 id="PWS_是如何工作的?">PWS 是如何工作的?</h2>
-
-<p>PWS通过容器来运行网站和应用已经有些年头了。Cloud Foundry一开始采用的容器技术名为Warden,现在使用的是名为Garden的容器系统。这些技术与流行的Docker容器很相似,而且事实上Cloud Foundry云平台的很多设施都支持部署Docker容器。</p>
-
-<p>使用Cloud Foundry云平台的好处之一是您不需要创建容器规范,因为Cloud Foundry的构建包会基于最新的组件来生成它们。由于容器是临时部署的,而且可能随时被清理并部署到云中的其他位置,所以Cloud Foundry云平台上的应用需要遵循<a href="https://12factor.net/zh_cn/">十二要素准则</a>。如此可以确保您的应用和平台使用最新的软件。一个应用程序可以包含多个实例,在这些实例中,应用程序被放置到冗余容器中,从而实现应用程序的高可用性。Cloud Foundry会自动处理相同实例之间的所有负载平衡。这允许您扩展应用程序的性能和可用性。</p>
-
-<p>由于文件系统是临时的,所以任何临时存储或服务都应该使用备份服务放置到其他地方。这可以通过使用不同提供商提供的市场服务或通过<a href="https://docs.run.pivotal.io/devguide/services/user-provided.html">用户自己提供的服务</a>来实现。</p>
-
-<h2 id="What_do_we_cover_below">What do we cover below?</h2>
-
-<p>This post covers how to modify the LocalLibrary application from the tutorial for deployment on PWS and Cloud Foundry. In doing so, it covers the basics of deploying any node.js application to PWS with the following steps.</p>
-
-<ul>
- <li>Configuring the package.json file to run with the engines available on PWS.</li>
- <li>Adding and installing the<a href="https://github.com/cloudfoundry-community/node-cfenv"> 'cfenv' node module</a> to make working with services easier.</li>
- <li>Using the cfenv module to connect to a MongoDB instance from mLab that was created and bound using the PWS marketplace.</li>
- <li>Using the <a href="https://github.com/cloudfoundry/cli">cf CLI</a> tool to create a new mongoDB service instance and bind it to the local library application.</li>
- <li>How to set environment variables for Node using the cf CLI.</li>
- <li>How to look at logs, again using the cf CLI tool.</li>
-</ul>
-
-<p>So let's get started. You have two options, you can go through the tutorial from the <a href="&lt;https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/Tutorial_local_library_website>">beginning</a> or you can just download the completed project and modify it from there for use on PWS. To do the latter, you can do the following from a terminal:</p>
-
-<pre class="brush: bash notranslate"><code>git clone https://github.com/mdn/express-locallibrary-tutorial</code></pre>
-
-<p>You'll then need to follow the preparation steps listed in the <a href="/en-US/docs/Learn/Server-side/Express_Nodejs/deployment#Getting_your_website_ready_to_publish">Getting your website ready to publish</a> section of <a href="/en-US/docs/Learn/Server-side/Express_Nodejs/deployment">Express Tutorial Part 7: Deploying to production</a>, before then following the steps listed below.</p>
-
-<div class="note">
-<p><strong>Note</strong>: This work flow is based on the <a href="/en-US/docs/Learn/Server-side/Express_Nodejs/deployment#Example_Installing_LocalLibrary_on_Heroku">Mozilla Heroku work flow in the main Express/Node tutorial series</a> for consistency, to help readers compare and contrast. </p>
-</div>
-
-<h2 id="Modifying_the_LocalLibrary_for_PWS">Modifying the LocalLibrary for PWS</h2>
-
-<p id="How_does_PWS_work">Deployment of a Node application on Cloud Foundry takes the following steps. Using the downloaded 'cf' CLI tool on your environment, your source code and supporting metadata files are uploaded to Cloud Foundry which will assemble and package the components of your application. Note that your files need to be located on your system to deploy or as a zip archive somewhere accessible on the internet. We'll assume the former.</p>
-
-<p>This means, no assumptions about which source control system is used. As long as you have a complete source tree in your local file system you can deploy the app. There are some things you have to make available to ensure the correctly assembly of your Node application. First Cloud Foundry cf CLI will look for the presence of the 'package.json' file to determine the dependencies and download the necessary components. The rules of this assembly are defined in Cloud Foundry's <a href="http://docs.run.pivotal.io/buildpacks/node/">nodejs buildpack</a>. An optional cloud foundry manifest file can specify information about your application such as name, size and start command if non-standard. In addition to deploying the application, the cf CLI tool can also configure services, set environment variables and view logs. That's all the overview you need in order to get started (see <a href="https://docs.run.pivotal.io">Getting Started on Pivotal Web Services</a> for a more comprehensive guide). Let's start making the changes so you'll need to deploy the <em>LocalLibrary</em> application to  PWS.</p>
-
-<h3 id="Set_node_version">Set node version</h3>
-
-<p>The <strong>package.json</strong> contains everything needed to work out your application dependencies and what file should be launched to start your site. Cloud Foundry and PWS detects the presence of this file, and will use it to provision your app environment. The only useful information missing in our current <strong>package.json</strong> is the version of node. We can find the version of node we're using for development by entering the command:</p>
-
-<pre class="brush: bash notranslate">node --version
-# <em>will return version e.g. v6.10.3</em></pre>
-
-<p>Open <strong>package.json</strong> with a text editor, and add this information as an <strong>engines &gt; node</strong> section as shown (using the version number retrieved above).</p>
-
-<pre class="brush: json notranslate">{
- "name": "express-locallibrary-tutorial",
- "version": "0.0.0",
-<strong> "engines": {
- "node": "6.10.3"
- },</strong>
- "private": true,
-  ...
-</pre>
-
-<h3 id="Database_configuration">Database configuration</h3>
-
-<p>So far in this tutorial we've used a single database that is hard coded into the <strong>app.js </strong>file. Normally we'd like to be able to have a different database for production and development, so next we'll modify the LocalLibrary website to get the database URI from the OS environment, and otherwise use our development database that we added manually earlier. Cloud Foundry has a very flexible services model that enables multiple services of the same type to exist in the environment. It stores all services related configurations in a single parseable JSON object called <code>VCAP_SERVICES</code>. A typical <code>VCAP_SERVICES</code> variable looks like this:</p>
-
-<pre class="brush: json notranslate">{
- "VCAP_SERVICES": {
- "mlab": [
- {
- "credentials": {
- "uri": "mongodb://CloudFoundry_test_dev:somecr8zypassw0rd@dbhost.mlab.com:57971/CloudFoundry_dbname"
- },
- "label": "mlab",
- "name": "node-express-tutorial-mongodb",
- "plan": "sandbox",
- "provider": null,
- "syslog_drain_url": null,
- "tags": [
- "Cloud Databases",
- "Developer Tools",
- "Web-based",
- "Data Store",
- ],
- "volume_mounts": []
- }
- ]
- }
-}
-
-</pre>
-
-<p>Writing the code to extract and parse this environment variable is not hard, but it doesn't add a lot of value when others have written libraries to do this. In this case, there is a node.js package we can use called <a href="https://github.com/cloudfoundry-community/node-cfenv"><em>cfenv</em></a>.</p>
-
-<p>This will download the cfenv module and its dependencies, and modify the package.json file as required. Open <strong>app.js</strong> and find the block with all the 'requires' that load the modules into your application. In this example look for the line that looks something like this:</p>
-
-<pre class="brush: js notranslate">var expressValidator = require('express-validator');</pre>
-
-<p>If you cannot find that exact line, look for the blocks of 'requires' and look for the last one. Add the following text after it:</p>
-
-<pre class="brush: js notranslate">var cfenv = require('cfenv');</pre>
-
-<ol>
- <li>
- <p>To install the package, go to your terminal and make sure you are in the directory where the <code>package.json</code> file for LocalLibrary is. From the command line, type:</p>
-
- <pre class="brush: bash notranslate">npm install cfenv</pre>
- </li>
- <li>
- <p>Now that you have loaded the module, this next line will instantiate an object that will contain the app and services information required for deployment. Add the following after the line that contains <code>app.use(helmet());</code></p>
-
- <pre class="brush: js notranslate">// Set up CF environment variables
-var appEnv = cfenv.getAppEnv();
-</pre>
-
- <p>When this line executes, all the Cloud Foundry application environment information will become available to the application in the <code>appEnv</code> object.</p>
- </li>
- <li>
- <p>Now it is time to update the application to use the database configured by the platform. Find the line that sets the mongoDB connection variable. It will look something like this:</p>
-
- <pre class="brush: js notranslate">var mongoDB = process.env.MONGODB_URI || dev_db_url;</pre>
- </li>
- <li>
- <p>You will now modify the line with the following code  <code>appEnv.getServiceURL('node-express-tutorial-mongodb')</code> to get the connection string from an environment variable that is being managed by the <code>cfenv</code>  module. If no service has been created and bound it will use your own database URL you created as part of the tutorial instead of the one from the environment. So replace the line above with the following:</p>
-
- <pre class="brush: js notranslate">var mongoDB = appEnv.getServiceURL('node-express-tutorial-mongodb') || dev_db_url;
-</pre>
- </li>
- <li>
- <p>Now run the site locally (see <a href="https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/routes#Testing_the_routes">Testing the routes</a> for the relevant commands) and check that the site still behaves as you expect. At this point your app is ready to use with Cloud Foundry and Pivotal Web Services.</p>
- </li>
-</ol>
-
-<h2 id="Get_a_Pivotal_Web_Services_account">Get a Pivotal Web Services account</h2>
-
-<p>To start using Pivotal Web Services you will first need to create an account (skip ahead to <a href="#Create_and_upload_the_website">Create and upload the website</a> if you've already got an account and have already installed the PWS cf CLI client).</p>
-
-<ul>
- <li>Go to <a href="https://run.pivotal.io">https://run.pivotal.io</a> and click the <strong>SIGN UP FOR FREE</strong> button.</li>
- <li>Enter your details and then press <strong>CREATE FREE ACCOUNT</strong>. You'll be asked to check your email for a sign-up email.</li>
- <li>Click the account activation link in the signup email. You'll be taken back to your account on the web browser and you will complete the registration.</li>
- <li>You will set your password and go through the rest of the new user sign up and how to claim your free trial account. Note you need a mobile phone to confirm your account. You will receive an "org" account funded with $87 of application usage credit. Note your email account can be in multiple orgs on PWS. This is similar to your user account on services like GitHub.</li>
- <li>Go to <a href="https://login.run.pivotal.io">https://console.run.pivotal.io</a> and login in. You'll then be logged in and taken to the PWS dashboard: <a href="https://console.run.pivotal.io">https://console.run.pivotal.io</a>.</li>
-</ul>
-
-<h2 id="Install_the_cf_CLI_client">Install the cf CLI client</h2>
-
-<p>The cf CLI client is a software tool for managing and deploying your application. Download and install the PWS cf CLI client by following the <a href="https://console.run.pivotal.io/tools">instructions on Pivotal Web Services</a> or downloading directly from <a href="https://github.com/cloudfoundry/cli">GIthub</a>. Be sure to download the correct version for your computer. After the client is installed you will be able run commands, for example to get help on the client:</p>
-
-<pre class="brush: bash notranslate">cf help
-</pre>
-
-<p>We'll now go through the steps to login to PWS using the CLI and deploy — or in Cloud Foundry parlance "push" your app.</p>
-
-<h2 id="Create_and_upload_the_website">Create and upload the website</h2>
-
-<p>To create the app we navigate to the directory where our modified files are. This is the same directory where the LocalLibrary package.json file is located. First, let's tell the cf CLI which Cloud Foundry instance you want to use. We need to do this, since the cf CLI tool can be used with any standard Cloud Foundry system, so this command indicates which specific Cloud Foundry you are using. Enter the following terminal command now:</p>
-
-<pre class="brush: bash notranslate">cf api api.run.pivotal.io</pre>
-
-<p>Next login using the following command (enter your email and password when prompted):</p>
-
-<pre class="brush: bash notranslate">cf login
-Email: enter your email
-Password: enter your password</pre>
-
-<p>We can now push our app to PWS. In the below example. replace 'some-unique-name' with something you can remember that is likely to be unique. If it isn't unique, the system will let you know. The reason this name has to be unique to the PWS system is it is the address we will use to to access your LocalLibrary application. I used <em>mozilla-express-tutorial-xyzzy</em>. You should use something else.</p>
-
-<pre class="brush: bash notranslate">cf push some-unique-name -m 256MB</pre>
-
-<p>Note the <code>-m</code> flag we added is not required. We just included it so that we only use 256MB of memory to run the app. Node apps typically can run in 128 MB, but we are being safe. If we didn't specify the memory, the CLI would use the default 1 GB of memory, but we want to make sure your trial lasts longer. You should now see a bunch of text on the screen. It will indicate that the CLI is uploading all your files, that it's using the node buildpack, and it will start the app. If we're lucky, the app is now "running" on the site at the URL <code>https://some-unique-name.cfapps.io</code>. Open your browser and run the new website by going to that URL.</p>
-
-<div class="note"><strong>Note</strong>: The site will be running using our hardcoded development database at this time. Create some books and other objects, and check out whether the site is behaving as you expect. In the next section we'll set it to use our new database.</div>
-
-<h2 id="Setting_configuration_variables">Setting configuration variables</h2>
-
-<p>You will recall from a preceding section that we need to <a href="#NODE_ENV">set NODE_ENV to 'production'</a> in order to improve our performance and generate less-verbose error messages.</p>
-
-<ol>
- <li>
- <p>Do this by entering the following command:</p>
-
- <pre class="brush: bash notranslate">cf set-env some-unique-name NODE_ENV production
-</pre>
- </li>
- <li>
- <p>We should also use a separate database for production. Cloud Foundry can take advantage of a marketplace to create a new service and automatically bind it to your app. Bind means place the service database credentials in the environment variable space of the container running your application for you to access. Enter the following commands:</p>
-
- <pre class="brush: bash notranslate">cf create-service mlab sandbox node-express-tutorial-mongodb
-cf bind-service some-unique-name node-express-tutorial-mongodb
-</pre>
- </li>
- <li>
- <p>You can inspect your configuration variables at any time using the <code>cf env some-unique-name</code> command — try this now:</p>
-
- <pre class="brush: bash notranslate">cf env some-unique-name
-</pre>
- </li>
- <li>
- <p>In order for your applications to use the new credentials you will have to restage your application, meaning that it will restart and apply the new environment variables. This can be done using the following — enter this command now:</p>
-
- <pre class="brush: bash notranslate">cf restage some-unique-name
-</pre>
- </li>
- <li>
- <p>If you check the home page now it should show zero values for your object counts, as the changes above mean that we're now using a new (empty) database.</p>
- </li>
-</ol>
-
-<h2 id="Debugging">Debugging</h2>
-
-<p>The PWS cf client provides a few tools for debugging:</p>
-
-<pre class="brush: bash notranslate">&gt;cf logs some-unique-name --recent # Show current logs
-&gt;cf logs some-unique-name # Show current logs and keep updating with any new results</pre>
-
-<h2 id="Summary">Summary</h2>
-
-<p>If you followed the above steps, you should have now deployed the LocalLibrary app to PWS. Well done! If the deployment wasn't successful, double check all the steps.</p>