创建Node.js APP
bash
npx express-generator lcApp --view ejs
cd lcApp
npm i
npm start
这时就可以通过3000端口来测试这个APP了。
部署Node.js APP
创建Resource Group
bash
az group create -l uksouth -n nodeAppResourceGroup
创建用于部署的用户
检查一下是否有用于部署的用户:
bash
az webapp deployment user show
如果返回的publishingUserName值为null,则说明没有由于部署的用户。那就需要创建用户:
bash
az webapp deployment user set --user-name YOUR_USERNAME --password YOUR_PASSWORD
需要注意上面的用户名需要全局唯一,也就是说不能和别人的用户名一样。
创建App Service Plan
bash
az appservice plan create --name TestNodeAppServicePlan --resource-group nodeAppResourceGroup --sku FREE
如果想要创建Linux App Service Plan,就必须指定**–is-linux**参数:
bash
az appservice plan create --name LinuxTestNodeAppServicePlan --resource-group nodeAppResourceGroup --is-linux --sku FREE
创建Web App
检查支持的运行时:
bash
az webapp list-runtimes
检查Linux的运行时:
bash
az webapp list-runtimes --os-type=linux
创建一个Web App:
bash
az webapp create --resource-group nodeAppResourceGroup --plan TestNodeAppServicePlan --name LcTestNodeApp --runtime "NODE:18LTS"
等到创建成功之后,就能通过访问该Web App了:https://lctestnodeapp.azurewebsites.net
如果想从本地Git仓库直接自动部署,需要添加–deployment-local-git。比如运行如下命令:
bash
az webapp create --resource-group nodeAppResourceGroup --plan TestNodeAppServicePlan --name LcTestNodeApp --runtime "NODE:18LTS" --deployment-local-git
如果想将应用部署到Linux,运行(目前Node 18似乎有些问题):
bash
az webapp create --resource-group nodeAppResourceGroup --plan TestNodeAppServicePlan --name LcTestNodeApp --runtime "NODE:16-lts" --deployment-local-git
将会返回一个git的local url:
bash
"deploymentLocalGitUrl": "https://[email protected]/LcTestNodeFrontEnd.git",
将本地git repo和远程的azure repo关联:
bash
git remote add azure https://[email protected]/LcTestNodeFrontEnd.git
推送代码:
bash
git push azure master
需要注意的是,在azure中,默认的部署分支仍然是master,因此如果你局部git的默认分支为main,则需要进行切换。
这时需要输入前面创建的部署用户的密码:

image
部署成功后可以看到提示:
bash
remote: Generating deployment script for node.js Web Site
remote: Generated deployment script files
remote: Running deployment command...
remote: Handling node.js deployment.
remote: Creating app_offline.htm
remote: KuduSync.NET from: 'C:\home\site\repository' to: 'C:\home\site\wwwroot'
remote: Deleting file: 'hostingstart.html'
remote: Copying file: '.gitignore'
remote: Copying file: 'app.js'
remote: Copying file: 'package-lock.json'
remote: Copying file: 'package.json'
remote: Copying file: 'bin\www'
remote: Copying file: 'public\stylesheets\style.css'
remote: Copying file: 'routes\index.js'
remote: Copying file: 'routes\users.js'
remote: Copying file: 'views\error.ejs'
remote: Copying file: 'views\index.ejs'
remote: Deleting app_offline.htm
remote: Using start-up script bin/www from package.json.
remote: Generated web.config.
remote: The package.json file does not specify node.js engine version constraints.
remote: The node.js application will run with the default node.js version 14.20.0.
remote: Selected npm version 6.14.17
remote: .....
remote: npm WARN read-shrinkwrap This version of npm is compatible with lockfileVersion@1, but package-lock.json was generated for lockfileVersion@2. I'll try to do my best with it!
remote: .......
remote: added 105 packages from 48 contributors and audited 105 packages in 11.189s
remote:
remote: 9 packages are looking for funding
remote: run `npm fund` for details
remote:
remote: found 0 vulnerabilities
remote:
remote: Finished successfully.
remote: Running post deployment command(s)...
remote: Triggering recycle (preview mode disabled).
remote: Deployment successful.
再查看app地址的话,就成为Express的默认页面了:https://lctestnodefrontend.azurewebsites.net/

image
删除Resource Group
最后不要忘记删除这个Resource Group及组中所有资源。
bash
az group delete -n nodeAppResourceGroup