js写的程序如何上线到linux,将 Node.js 应用发布到 Linux 应用服务 - Visual Studio | Microsoft Docs...
將 Node.js 應(yīng)用程序發(fā)布到 Azure(Linux 應(yīng)用服務(wù))Publish a Node.js application to Azure (Linux App Service)
11/22/2019
本文內(nèi)容
本教程指導(dǎo)你完成創(chuàng)建簡(jiǎn)單 Node.js 應(yīng)用程序并將其發(fā)布到 Azure 的任務(wù)。This tutorial walks you through the task of creating a simple Node.js application and publishing it to Azure.
將 Node.js 應(yīng)用程序發(fā)布到 Azure 時(shí),提供幾個(gè)選項(xiàng)。When publishing a Node.js application to Azure, there are several options. 其中包括 Azure 應(yīng)用服務(wù)、運(yùn)行所選 OS 的 VM、用于 Kubernetes 管理的 Azure 容器服務(wù) (AKS)、使用 Docker 的容器實(shí)例等等。These include Azure App Service, a VM running an OS of your choosing, Azure Container Service (AKS) for management with Kubernetes, a Container Instance using Docker, and more. 有關(guān)各選項(xiàng)的詳細(xì)信息,請(qǐng)參閱計(jì)算。For more details on each of these options, see Compute.
對(duì)于本教程,將應(yīng)用部署到 Linux 應(yīng)用服務(wù)。For this tutorial, you deploy the app to Linux App Service.
Linux 應(yīng)用服務(wù)部署 Linux Docker 容器來(lái)運(yùn)行 Node.js 應(yīng)用程序(不同于 Windows 應(yīng)用服務(wù),后者在 Windows 上 IIS 后運(yùn)行 Node.js 應(yīng)用程序)。Linux App Service deploys a Linux Docker container to run the Node.js application (as opposed to the Windows App Service, which runs Node.js apps behind IIS on Windows).
本教程從隨針對(duì) Visual Studio 的 Node.js 工具安裝的一個(gè)模板開始,演示如何創(chuàng)建 Node.js 應(yīng)用程序,并將代碼推動(dòng)到 GitHub 上的存儲(chǔ)庫(kù),然后通過(guò) Azure Web 門戶預(yù)配 Azure 應(yīng)用服務(wù),以便可以從 GitHub 存儲(chǔ)庫(kù)進(jìn)行部署。This tutorial shows how to create a Node.js application starting from a template installed with the Node.js Tools for Visual Studio, push the code to a repository on GitHub, and then provision an Azure App Service via the Azure web portal so that you can deploy from the GitHub repository. 若要使用命令行來(lái)預(yù)配 Azure 應(yīng)用服務(wù)并從本地 Git 存儲(chǔ)庫(kù)推送代碼,請(qǐng)參閱創(chuàng)建 Node.js 應(yīng)用。To use the command-line to provision the Azure App Service and push the code from a local Git repository, see Create Node.js App.
在本教程中,你將了解:In this tutorial, you learn how to:
創(chuàng)建 Node.js 項(xiàng)目Create a Node.js project
為代碼創(chuàng)建 GitHub 存儲(chǔ)庫(kù)Create a GitHub repository for the code
創(chuàng)建 Azure 上的 Linux 應(yīng)用服務(wù)Create a Linux App Service on Azure
部署到 LinuxDeploy to Linux
先決條件Prerequisites
須安裝 Visual Studio 且具有 Node.js 開發(fā)工作負(fù)載。You must have Visual Studio installed and the Node.js development workload.
如果尚未安裝 Visual Studio 2019,請(qǐng)轉(zhuǎn)到 Visual Studio 下載頁(yè)免費(fèi)安裝。If you haven't already installed Visual Studio 2019, go to the Visual Studio downloads page to install it for free.
如果尚未安裝 Visual Studio 2017,請(qǐng)轉(zhuǎn)到 Visual Studio 下載頁(yè)免費(fèi)安裝。If you haven't already installed Visual Studio 2017, go to the Visual Studio downloads page to install it for free.
如果需要安裝工作負(fù)載但已有 Visual Studio,請(qǐng)轉(zhuǎn)到“工具” > “獲取工具和功能...”,這會(huì)打開 Visual Studio 安裝程序。If you need to install the workload but already have Visual Studio, go to Tools > Get Tools and Features..., which opens the Visual Studio Installer. 選擇“Node.js 開發(fā)”工作負(fù)載,然后選擇“修改” 。Choose the Node.js development workload, then choose Modify.
須安裝 Node.js 運(yùn)行時(shí)。You must have the Node.js runtime installed.
如果未安裝,請(qǐng)從 Node.js 網(wǎng)站安裝 LTS 版本。If you don't have it installed, install the LTS version from the Node.js website. 一般情況下,Visual Studio 會(huì)自動(dòng)檢測(cè)已安裝的 Node.js 運(yùn)行時(shí)。In general, Visual Studio automatically detects the installed Node.js runtime. 如果系統(tǒng)未檢測(cè)到已安裝運(yùn)行時(shí),則可以將項(xiàng)目配置為引用屬性頁(yè)中已安裝的運(yùn)行時(shí)(創(chuàng)建項(xiàng)目后,右鍵單擊項(xiàng)目節(jié)點(diǎn)并選擇“屬性”)。If it does not detect an installed runtime, you can configure your project to reference the installed runtime in the properties page (after you create a project, right-click the project node and choose Properties).
創(chuàng)建要在 Azure 中運(yùn)行的 Node.js 項(xiàng)目Create a Node.js project to run in Azure
打開 Visual Studio。Open Visual Studio.
創(chuàng)建新的 TypeScript Express 應(yīng)用。Create a new TypeScript Express app.
按 Esc 關(guān)閉啟動(dòng)窗口。Press Esc to close the start window. 鍵入 Ctrl+Q 以打開搜索框,鍵入“Node.js”,然后選擇“創(chuàng)建新的基本 Azure Node.js Express 4 應(yīng)用程序”(TypeScript) 。Type Ctrl + Q to open the search box, type Node.js, then choose Create new Basic Azure Node.js Express 4 application (TypeScript). 在出現(xiàn)的對(duì)話框中,選擇“創(chuàng)建”。In the dialog box that appears, choose Create.
從頂部菜單欄中選擇“文件” > “新建” > “項(xiàng)目”。From the top menu bar, choose File > New > Project. 在“新建項(xiàng)目”對(duì)話框的左窗格中,展開“TypeScript”,然后選擇“Node.js” 。In the left pane of the New Project dialog box, expand TypeScript, then choose Node.js. 在中間窗格中,選擇“基本 Azure Node.js Express 4 應(yīng)用程序”,然后選擇“確定” 。In the middle pane, choose Basic Azure Node.js Express 4 application, then choose OK.
如果未看到“基本 Azure Node.js Express 4 應(yīng)用程序”項(xiàng)目模板,必須添加 Node.js 開發(fā)工作負(fù)載 。If you don't see the Basic Azure Node.js Express 4 application project template, you must add the Node.js development workload. 有關(guān)詳細(xì)說(shuō)明,請(qǐng)參閱先決條件。For detailed instructions, see the Prerequisites.
Visual Studio 將創(chuàng)建項(xiàng)目并在解決方案資源管理器(右窗格)中將其打開。Visual Studio creates the project and opens it in Solution Explorer (right pane).
按 F5 來(lái)生成并運(yùn)行應(yīng)用,確保一切按預(yù)期運(yùn)行。Press F5 to build and run the app, and make sure that everything is running as expected.
選擇“文件” > “添加到源代碼管理”,創(chuàng)建項(xiàng)目的本地 Git 存儲(chǔ)庫(kù)。Select File > Add to source control to create a local Git repository for the project.
此時(shí),采用 Express 框架且以 TypeScript 編寫的 Node.js 應(yīng)用正常工作并簽入到本地源代碼管理。At this point, a Node.js app using the Express framework and written in TypeScript is working and checked in to local source control.
根據(jù)需要編輯項(xiàng)目,再繼續(xù)到下一步驟。Edit the project as desired before proceeding to the next steps.
將代碼從 Visual Studio 推送到 GitHubPush code from Visual Studio to GitHub
設(shè)置用于 Visual Studio 的 GitHub:To set up GitHub for Visual Studio:
Make sure the GitHub Extension for Visual Studio is installed and enabled using the menu item Tools > Extensions and Updates.
從菜單中選擇“視圖” > “其他窗口” > “GitHub”。From the menu select View > Other Windows > GitHub.
GitHub 窗口隨即打開。The GitHub window opens.
如果在 GitHub 窗口中沒(méi)有看到“開始操作”按鈕,請(qǐng)單擊“文件” > “添加到源代碼管理”并等待 UI 更新。If you don't see the Get Started button in the GitHub window, click File > Add to Source Control and wait for the UI to update.
單擊“開始操作”。Click Get started.
如果已連接到 GitHub,工具箱將顯示,類似于下圖。If you are already connected to GitHub, the toolbox appears similar to the following illustration.
填寫字段使新存儲(chǔ)庫(kù)得以發(fā)布,然后單擊“發(fā)布”。Complete the fields for the new repository to publish, and then click Publish.
幾分鐘后會(huì)顯示一個(gè)橫幅,指出“已成功創(chuàng)建存儲(chǔ)庫(kù)”。After a few moments, a banner stating "Repository created successfully" appears.
下一步部分中,將學(xué)習(xí)如何從此存儲(chǔ)庫(kù)發(fā)布到 Linux 上 Azure 應(yīng)用服務(wù)。In the next section, you learn how to publish from this repository to an Azure App Service on Linux.
創(chuàng)建 Azure 中的 Linux 應(yīng)用服務(wù)Create a Linux App Service in Azure
從左側(cè)服務(wù)列表中選擇“應(yīng)用服務(wù)”,然后單擊“添加”。Select App Services from the list of services on the left, and then click Add.
如果要求,請(qǐng)創(chuàng)建新的資源組和應(yīng)用服務(wù)計(jì)劃來(lái)托管新應(yīng)用。If required, create a new Resource Group and App Service plan to host the new app.
確保將“OS”設(shè)為“Linux”,將“運(yùn)行時(shí)堆棧”設(shè)為所需 Node.js 版本,如圖所示 。Make sure to set the OS to Linux, and set Runtime Stack to the required Node.js version, as shown in the illustration.
單擊“創(chuàng)建”以創(chuàng)建應(yīng)用服務(wù)。Click Create to create the App Service.
部署可能需要幾分鐘的時(shí)間。It may take a few minutes to deploy.
部署后,請(qǐng)轉(zhuǎn)到“應(yīng)用程序設(shè)置”部分,添加一個(gè)名為 SCM_SCRIPT_GENERATOR_ARGS、值為 --node 的設(shè)置。After it is deployed, go to the Application settings section, and add a setting with a name of SCM_SCRIPT_GENERATOR_ARGS and a value of --node.
警告
應(yīng)用服務(wù)部署過(guò)程使用一組試探法來(lái)確定要嘗試和運(yùn)行哪種類型的應(yīng)用程序。The App Service deployment process uses a set of heuristics to determine which type of application to try and run. 如果在已部署的內(nèi)容中檢測(cè)到 .sln 文件,則假設(shè)正在部署基于 MSBuild 的項(xiàng)目。If a .sln file is detected in the deployed content, it will assume an MSBuild based project is being deployed. 上面添加的設(shè)置將覆蓋此邏輯,并顯式指定這是 Node.js 應(yīng)用程序。The setting added above overrides this logic and specifies explicitly that this is a Node.js application. 如果沒(méi)有此設(shè)置,當(dāng) .sln 文件屬于正部署到應(yīng)用服務(wù)的存儲(chǔ)庫(kù)時(shí),Node.js 應(yīng)用程序?qū)⒉渴鹗 ithout this setting, the Node.js application will fail to deploy if the .sln file is part of the repository being deployed to the App Service.
在“應(yīng)用程序設(shè)置”下,添加另一個(gè)名稱為 WEBSITE_NODE_DEFAULT_VERSION 且值為 8.9.0 的設(shè)置。Under Application settings, add another setting with a name of WEBSITE_NODE_DEFAULT_VERSION and a value of 8.9.0.
部署后,打開應(yīng)用服務(wù),并選擇“部署選項(xiàng)”。After it is deployed, open the App Service and select Deployment options.
單擊“選擇源”,選擇“GitHub”,然后配置任何所需的權(quán)限。Click Choose source, and then choose GitHub, and then configure any required permissions.
選擇要發(fā)布的存儲(chǔ)庫(kù)和分支,并選擇“確定”。Select the repository and branch to publish, and then select OK.
同步時(shí)“部署選項(xiàng)”頁(yè)將出現(xiàn)。The deployment options page appears while syncing.
完成同步后,將出現(xiàn)一個(gè)復(fù)選標(biāo)記。Once it is finished syncing, a check mark will appear.
站點(diǎn)現(xiàn)在運(yùn)行來(lái)自 GitHub 存儲(chǔ)庫(kù)的 Node.js 應(yīng)用程序,可通過(guò)為 Azure 應(yīng)用服務(wù)創(chuàng)建的 URL 進(jìn)行訪問(wèn)(默認(rèn)情況下為 Azure 應(yīng)用服務(wù)提供的名稱后跟“.azurewebsites.net”)。The site is now running the Node.js application from the GitHub repository, and it is accessible at the URL created for the Azure App Service (by default the name given to the Azure App Service followed by ".azurewebsites.net").
修改應(yīng)用并推送更改Modify your app and push changes
在 app.use('/users', users); 行之后,將此處顯示的代碼添加到 app.ts。Add the code shown here in app.ts after the line app.use('/users', users);. 這會(huì)在 URL /api 處添加 REST API。This adds a REST API at the URL /api.
app.use('/api', (req, res, next) => {
res.json({"result": "success"});
});
生成代碼并進(jìn)行本地測(cè)試,然后將其簽入并推送到 GitHub。Build the code and test it locally, then check it in and push to GitHub.
在 Azure 門戶中,需要一些時(shí)間來(lái)檢測(cè) GitHub 存儲(chǔ)庫(kù)中的更改,然后開始再次同步部署。In the Azure portal, it takes a few moments to detect changes in the GitHub repo, and then a new sync of the deployment starts. 它看上去類似于下圖。This looks similar to the following illustration.
部署完成后,導(dǎo)航到公共站點(diǎn)并向 URL 追加 /api。Once deployment is complete, navigate to the public site and append /api to the URL. 返回 JSON 響應(yīng)。The JSON response gets returned.
疑難解答Troubleshooting
如果 node.exe 進(jìn)程發(fā)生故障(也就是說(shuō)出現(xiàn)未處理的異常),容器將重新啟動(dòng)。If the node.exe process dies (that is, an unhandled exception occurs), the container restarts.
容器啟動(dòng)時(shí),它通過(guò)各種試探法來(lái)找出如何啟動(dòng) Node.js 進(jìn)程。When the container starts up, it runs through various heuristics to figure out how to start the Node.js process. Details of the implementation can be seen at generateStartupCommand.js.
可以通過(guò) SSH 連接到正在運(yùn)行的容器進(jìn)行調(diào)查。You can connect to the running container via SSH for investigations. 這可以通過(guò) Azure 門戶輕松實(shí)現(xiàn)。This is easily done using the Azure portal. 選擇應(yīng)用服務(wù),向下滾動(dòng)工具列表直至“開發(fā)工具”部分下的“SSH” 。Select the App Service, and scroll down the list of tools until reaching SSH under the Development Tools section.
若要幫助進(jìn)行故障排除,請(qǐng)轉(zhuǎn)到應(yīng)用服務(wù)的“診斷日志”設(shè)置,并將“Docker 容器日志記錄”設(shè)置從“關(guān)”改為“文件系統(tǒng)” 。To aid in troubleshooting, go to the Diagnostics logs settings for the App Service, and change the Docker Container logging setting from Off to File System. 日志創(chuàng)建在 /home/LogFiles/_docker.log* 中,并且可以通過(guò) SSH 或 FTP 在框上進(jìn)行訪問(wèn)。Logs are created in the container under /home/LogFiles/ _docker.log*, and can be accessed on the box using SSH or FTP(S).
可向站點(diǎn)分配自定義域名,而不是默認(rèn)分配的 *.azurewebsites.net URL。A custom domain name may be assigned to the site, rather than the *.azurewebsites.net URL assigned by default. 有關(guān)更多詳細(xì)信息,請(qǐng)參閱主題映射自定義域。For more details, see the topic Map Custom Domain.
最好先部署到過(guò)渡站點(diǎn)進(jìn)行進(jìn)一步測(cè)試,再移動(dòng)到生產(chǎn)環(huán)節(jié)。Deploying to a staging site for further testing before moving into production is a best practice. 有關(guān)如何配置此項(xiàng)的詳細(xì)信息,請(qǐng)參閱主題創(chuàng)建過(guò)渡環(huán)境。For details on how to configure this, see the topic Create staging environments.
See the App Service on Linux FAQ for more commonly asked questions.
后續(xù)步驟Next steps
本教程中,你學(xué)習(xí)了如何創(chuàng)建 Linux 應(yīng)用服務(wù)并將 Node.js 應(yīng)用程序部署到該服務(wù)。In this tutorial, you learned how create a Linux App Service and deploy a Node.js application to the service. 你可能想要詳細(xì)了解 Linux 應(yīng)用服務(wù)。You may want to learn more about Linux App Service.
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的js写的程序如何上线到linux,将 Node.js 应用发布到 Linux 应用服务 - Visual Studio | Microsoft Docs...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 黑美人鱼女主曾遭网暴 迪士尼《小美人鱼》
- 下一篇: windows下的库文件在linux的使