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