如何将 Node.js Web 应用部署到 Azure Stack 中的 VMHow to deploy a Node.js web app to a VM in Azure Stack

可以创建一个 VM 来托管 Azure Stack 中的 Node.js Web 应用。You can create a VM to host your Node.js Web app in Azure Stack. 本文介绍在设置服务器、配置服务器以托管 Node Web 应用以及随后部署应用时将遵循的步骤。This article looks at the steps you will follow in setting up server, configuring the server to host your Node web app, and then deploying your app.

Node.js 是构建在 Chrome V8 JavaScript 引擎基础之上的 JavaScript 运行时。Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. 作为异步事件驱动的 JavaScript 运行时,Node 旨在构建可缩放的网络应用程序。As an asynchronous event driven JavaScript runtime, Node is designed to build scalable network applications. 若要了解 Node 编程语言并找到适用于 Node 的其他资源,请参阅 Nodejs.orgTo learn the Node programming language and find additional resources for Node, see Nodejs.org.

创建 VMCreate a VM

  1. 在 Azure Stack 中设置 VM。Set up your VM set up in Azure Stack.

  2. 在“VM 网络”边栏选项卡中,确保以下端口可访问:In the VM network blade, make sure the following ports are accessible:

    端口Port 协议Protocol 说明Description
    8080 HTTPHTTP 超文本传输协议 (HTTP) 是一种适用于分布式协作型超媒体信息系统的应用程序协议。Hypertext Transfer Protocol (HTTP) is an application protocol for distributed, collaborative, hypermedia information systems. 客户端将使用 VM 的公共 IP 或 DNS 名称连接到你的 Web 应用。Clients will connect to your web app with either the public IP or DNS name of your VM.
    443443 HTTPSHTTPS 安全超文本传输协议 (HTTPS) 是超文本传输协议 (HTTP) 的扩展。Hypertext Transfer Protocol Secure (HTTPS) is an extension of the Hypertext Transfer Protocol (HTTP). 它用于通过计算机网络进行安全通信。It is used for secure communication over a computer network. 客户端将使用 VM 的公共 IP 或 DNS 名称连接到你的 Web 应用。Clients will connect to your web app with the either the public IP or DNS name of your VM.
    2222 SSHSSH 安全外壳 (SSH) 是一种加密网络协议,用于在不安全的网络上安全地运行网络服务。Secure Shell (SSH) is a cryptographic network protocol for operating network services securely over an unsecured network. 你将在 SSH 客户端上使用此连接来配置 VM 并部署应用。You will use this connection with an SSH client to configure the VM and deploy the app.
    33893389 RDPRDP 可选。Optional. 远程桌面协议允许远程桌面连接使用计算机的图形用户界面。The Remote Desktop Protocol allows for a remote desktop connection to use a graphic user interface your machine.
    13371337 “自定义”Custom Node.js 使用端口 1337。Port 1337 is used by the Node.js. 对于生产服务器,你需要通过 80 和 443 路由流量。For a production server, you will want to route your traffic through 80 and 443.

安装 NodeInstall Node

  1. 使用 SSH 客户端连接到 VM。Connect to your VM using your SSH client. 有关说明,请参阅使用 PuTTy 通过 SSH 连接For instructions, see Connect via SSH with PuTTy.

  2. 在 VM 上的 bash 提示符下,键入以下命令:At your bash prompt on your VM, type the following commands:

      sudo apt install nodejs-legacy
    
  3. 安装 NPM。Install NPM. NPM 是 Node.js 包或模块的包管理器。NPM is a package manager for Node.js packages, or modules. 仍在 SSH 会话中连接到 VM,键入以下命令:Still connected to your VM in your SSH session, type the following commands:

       go version
    
  4. 安装 Git。Install Git. Git 是一个广泛分布的修订控制和源代码管理 (SCM) 系统。Git is a widely distributed revision control and source code management (SCM) system. 仍在 SSH 会话中连接到 VM,键入以下命令:Still connected to your VM in your SSH session, type the following commands:

       sudo apt-get -y install git
    
  5. 验证你的安装。Validate your installation. 仍在 SSH 会话中连接到 VM,键入以下命令:Still connected to your VM in your SSH session, type the following commands:

       node -v
    

部署和运行应用Deploy and run the app

  1. 在 VM 上设置 Git 存储库。Set up your Git repository on the VM. 仍在 SSH 会话中连接到 VM,键入以下命令:Still connected to your VM in your SSH session, type the following commands:

       git clone https://github.com/Azure-Samples/nodejs-docs-hello-world.git
    
       cd nodejs-docs-hello-world
        npm start
    
  2. 启动应用。Start the app. 仍在 SSH 会话中连接到 VM,键入以下命令:Still connected to your VM in your SSH session, type the following command:

       sudo node app.js
    
  3. 现在导航到新服务器,应看到正在运行的 Web 应用程序。Now navigate to your new server and you should see your running web application.

       http://yourhostname.chinacloudapp.cn:1337
    

后续步骤Next steps