Azure上Node.js站点无法加载,重新发布后日志出现异常
Hey there, let's work through this issue—your Express app was running smoothly since March, but now it's throwing "Service Unavailable" after redeployment, and the logstream only shows partial deployment steps. Here are actionable steps to diagnose and fix this:
First, confirm the deployment finished successfully
The logs you shared cut off at "Preparing deployment for commit id '7579046ef...", which means we don't know if the deployment completed without errors. Head to the Azure Portal's Deployment Center for your app, find the latest deployment, and check the full log. Look for red flags like failednpm installcommands, missing dependencies, or a broken build script in yourpackage.json—any of these can halt deployment and leave your app in an unstartable state.Double-check your app's startup configuration
Azure needs explicit instructions to launch your Express app. Make sure yourpackage.jsonhas a validstartscript, e.g.:"scripts": { "start": "node server.js" }Replace
server.jswith your actual entry file. If you use a custom startup command, verify it's still correct in the Azure Portal under Configuration > General settings > Startup Command. Redeployments sometimes reset this setting, especially if you changed your entry file recently.Ensure your app listens on the correct port
Azure doesn't use the default 3000 port—it assigns a dynamic port via thePORTenvironment variable. If your code hardcodes port 3000, Azure can't route traffic to your app. Update your Express startup code to use:const port = process.env.PORT || 3000; app.listen(port, () => { console.log(`Server running on port ${port}`); });The fallback to 3000 lets you test locally, while
process.env.PORTensures compatibility with Azure.Pull full runtime logs to catch app startup errors
The logstream you checked only shows deployment logs. To see why your app isn't starting, enable Application Logging (Filesystem) in the Azure Portal under App Service > Logs, then restart your app. You can also use the Azure CLI commandaz webapp log tail --name <your-app-name> --resource-group <your-resource-group>to get real-time runtime logs. This will reveal issues like missing modules, syntax errors, or failed database connections that the deployment logs don't show.Check for resource limits
It's possible your App Service plan is hitting CPU or memory limits. Head to App Service > Metrics and monitor CPU, Memory, and Disk usage over the last few hours. If resources are maxed out, try scaling up your plan to a higher tier (e.g., from Free to Basic) or scaling out to add more instances. Also, check for unexpected background processes eating up resources.Roll back to a known working deployment
Since your app ran fine until today, roll back to a deployment from before the issue started. In the Azure Portal's Deployment Center > Deployments, select a successful deployment from March or early April, then click Rollback. This will revert your app to a stable state, helping you figure out if the problem comes from recent code changes or an Azure environment issue.
内容的提问来源于stack exchange,提问作者balexandre

