【发布时间】:2021-03-17 01:42:46
【问题描述】:
我有一个 nodejs/express 应用程序,我将其部署到 Linux 平台上的 Azure Appservice。 Azure 提供运行我的应用程序的端口 8080。 但是,当我部署它时,它给了我一个错误
Error listen EADDRINUSE: address already in use :::8181
error Command failed with exit code 1.
我运行应用程序的主要命令是env NODE_ENV=production node index.js。
应用使用的端口是8080。但是即将出现的错误是针对端口8181。我已经尝试重新启动服务器并重新部署了很多次。
我按照本教程进行部署。 https://docs.microsoft.com/en-us/azure/app-service/quickstart-nodejs?pivots=platform-linux
我删除了整个 appservice 及其计划并重新部署它,却发现同样的错误。
我 ssh 进入容器并检查 8181 是否正在使用中。但事实并非如此。
这是我启动应用程序的方式:
const port = 8080;
const server = app.listen(process.env.PORT || port, () => {
console.log(`App running on port ${port}`);
});
我该怎么办?
编辑:
这是我的 package.json
{
"name": "test-express",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon index.js",
"build:azure": "env NODE_ENV=production node index.js"
},
"dependencies": {
"express": "^4.17.1",
"morgan": "^1.10.0"
},
"devDependencies": {
"nodemon": "^2.0.7"
}
}
在我下面提到的官方文档中,启动应用程序的脚本之一是“build:azure”。
此外,我将这些行添加到我的代码中以查看传递了哪些环境变量。
console.log("=============");
console.log(process.env.PORT, process.env.NODE_ENV);
console.log("=============");
我得到的构建过程的最终结果是
...
7:00:08 pm test-express: [1/4] Resolving packages...
7:00:09 pm test-express: [2/4] Fetching packages...
7:00:30 pm test-express: info fsevents@2.3.2: The platform "linux" is incompatible with this module.
7:00:30 pm test-express: info "fsevents@2.3.2" is an optional dependency and failed compatibility check. Excluding it from installation.
7:00:30 pm test-express: [3/4] Linking dependencies...
7:00:45 pm test-express: [4/4] Building fresh packages...
7:00:45 pm test-express: Done in 37.21s.
7:00:45 pm test-express: Running 'yarn run build:azure'...
7:00:46 pm test-express: yarn run v1.22.10
7:00:46 pm test-express: $ env NODE_ENV=production node index.js
7:00:46 pm test-express: =============
7:00:46 pm test-express: 8181 production
7:00:46 pm test-express: =============
7:00:46 pm test-express: events.js:292
7:00:46 pm test-express: throw er; // Unhandled 'error' event
7:00:46 pm test-express: ^
7:00:46 pm test-express: info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
7:00:46 pm test-express: Error: listen EADDRINUSE: address already in use :::8181
7:00:46 pm test-express: at Server.setupListenHandle [as _listen2] (net.js:1318:16)
7:00:46 pm test-express: at listenInCluster (net.js:1366:12)
7:00:46 pm test-express: at Server.listen (net.js:1452:7)
7:00:46 pm test-express: at Function.listen (/tmp/8d8e87f8a5dd6db/node_modules/express/lib/application.js:618:24)
7:00:46 pm test-express: at Object.<anonymous> (/tmp/8d8e87f8a5dd6db/index.js:8:20)
7:00:46 pm test-express: at Module._compile (internal/modules/cjs/loader.js:1063:30)
7:00:46 pm test-express: at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
7:00:46 pm test-express: at Module.load (internal/modules/cjs/loader.js:928:32)
7:00:46 pm test-express: at Function.Module._load (internal/modules/cjs/loader.js:769:14)
7:00:46 pm test-express: at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
7:00:46 pm test-express: Emitted 'error' event on Server instance at:
7:00:46 pm test-express: at emitErrorNT (net.js:1345:8)
7:00:46 pm test-express: at processTicksAndRejections (internal/process/task_queues.js:80:21) {
7:00:46 pm test-express: code: 'EADDRINUSE',
7:00:46 pm test-express: errno: -98,
7:00:46 pm test-express: syscall: 'listen',
7:00:47 pm test-express: address: '::',
7:00:47 pm test-express: port: 8181
7:00:47 pm test-express: }
7:00:47 pm test-express: error Command failed with exit code 1.
7:00:49 pm test-express: /opt/Kudu/Scripts/starter.sh oryx build /tmp/zipdeploy/extracted -o /home/site/wwwroot --platform nodejs --platform-version 14 -i /tmp/8d8e87f8a5dd6db -p compress_node_modules=tar-gz --log-file /tmp/build-debug.log
7:01:07 pm test-express: Deployment failed.
所以是的,8181 正在通过。
已修复:
是的,我通过将脚本属性 build:azure 更改为 start 来修复它。这是我需要在 package.json 中进行的唯一更改才能使其运行。
【问题讨论】:
-
你能发布截图吗? appservice 不公开端口 8081
-
在整个项目中,全局搜索
8181看有没有相关代码,记得改用process.env.PORT || 8080。 -
@JasonPan 我对
8181进行了全球搜索,但什么也没找到。是的,我已经在使用process.env.PORT || 8080 -
试试我的方法,告诉我结果。
-
您的问题解决了吗?有进展吗?
标签: node.js azure express azure-web-app-service