问题描述
当我尝试在IIS服务器上安装nestJs应用程序(安装iisnode)时,我总是遇到以下问题: “ ...错误:监听EADDRINUSE:地址已在使用::: 3450 ..”(请参见下文) 而且,正如您从console.log中看到的那样(请参见下文),nestJs应用程序首先已完全初始化并启动,但始终会出现端口已在使用中的问题。无论我尝试多少个不同的端口,它都将持续存在。我也尝试过重新启动iisnode.exe并杀死了进程和端口,但这无济于事。任何人对此都有经验并找到了解决方案?
节点dist / main
[32m[nest] 36096 - [39m09/01/2020,1:43:54 PM [33m[nestFactory] [39m[32mStarting nest application...[39m
[32m[nest] 36096 - [39m09/01/2020,1:43:54 PM [33m[InstanceLoader] [39m[32mConfigHostModule dependencies initialized[39m[33m +34ms[39m
[32m[nest] 36096 - [39m09/01/2020,1:43:54 PM [33m[InstanceLoader] [39m[32mAppModule dependencies initialized[39m[33m +0ms[39m
[32m[nest] 36096 - [39m09/01/2020,1:43:54 PM [33m[InstanceLoader] [39m[32mConfigModule dependencies initialized[39m[33m +1ms[39m
hello from nestJs app
[32m[nest] 36096 - [39m09/01/2020,1:43:54 PM [33m[RoutesResolver] [39m[32mAppController {}:[39m[33m +13ms[39m
[32m[nest] 36096 - [39m09/01/2020,1:43:54 PM [33m[RouterExplorer] [39m[32mMapped {,GET} route[39m[33m +4ms[39m
[32m[nest] 36096 - [39m09/01/2020,1:43:54 PM [33m[RoutesResolver] [39m[32mItemsController {/items}:[39m[33m +0ms[39m
[32m[nest] 36096 - [39m09/01/2020,1:43:54 PM [33m[RouterExplorer] [39m[32mMapped {/items,GET} route[39m[33m +1ms[39m
[32m[nest] 36096 - [39m09/01/2020,1:43:54 PM [33m[RouterExplorer] [39m[32mMapped {/items/:id,GET} route[39m[33m +2ms[39m
[32m[nest] 36096 - [39m09/01/2020,POST} route[39m[33m +1ms[39m
[32m[nest] 36096 - [39m09/01/2020,1:43:54 PM [33m[RoutesResolver] [39m[32mShoppingCartController {/shopping-cart}:[39m[33m +1ms[39m
[32m[nest] 36096 - [39m09/01/2020,1:43:54 PM [33m[RouterExplorer] [39m[32mMapped {/shopping-cart,POST} route[39m[33m +0ms[39m
[32m[nest] 36096 - [39m09/01/2020,1:43:54 PM [33m[nestApplication] [39m[32mnest application successfully started[39m[33m +4ms[39m
App is running in port: 61302
events.js:292
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::61302
at Server.setupListenHandle [as _listen2] (net.js:1313:16)
at listenInCluster (net.js:1361:12)
at Server.listen (net.js:1447:7)
at ExpressAdapter.listen (C:\...\node_modules\@nestjs\platform-express\adapters\express-adapter.js:45:32)
at nestApplication.listen (C:\...\node_modules\@nestjs\core\nest-application.js:146:26)
at async bootstrap (C:\...\dist\main.js:8:9)
Emitted 'error' event on Server instance at:
at emitErrorNT (net.js:1340:8)
at processticksAndRejections (internal/process/task_queues.js:84:21) {
code: 'EADDRINUSE',errno: 'EADDRINUSE',syscall: 'listen',address: '::',port: 61302
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! nes[email protected] start:prod: `node dist/main`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the nes[email protected] start:prod script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\...\AppData\Roaming\npm-cache\_logs\2020-09-01T12_43_54_285Z-debug.log
在iisnode服务器错误日志中,我还可以找到以下内容: (这证明是在前一天记录的-不再在日志中显示。因此,此iisnode日志消息可能根本与问题无关)
(node:45472) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(),Buffer.allocUnsafe(),or Buffer.from() methods instead.
(node:45472) UnhandledPromiseRejectionWarning: RangeError [ERR_SOCKET_BAD_PORT]: options.port should be >= 0 and < 65536. Received NaN.
at validatePort (internal/validators.js:182:11)
at Server.listen (net.js:1439:5)
at ExpressAdapter.listen (C:\...\node_modules\@nestjs\platform-express\adapters\express-adapter.js:45:32)
at nestApplication.listen (C:\...\node_modules\@nestjs\core\nest-application.js:146:26)
at async bootstrap (C:\...\dist\main.js:7:5)
(node:45472) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block,or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection,use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:45472) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future,promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
此外,这是当前格式的/dist/main.js文件(我已经添加了try / catch -statements)。
"use strict";
Object.defineProperty(exports,"__esModule",{ value: true });
const core_1 = require("@nestjs/core");
const app_module_1 = require("./app.module");
async function bootstrap() {
try {
const app = await core_1.nestFactory.create(app_module_1.AppModule);
await app.listen(parseInt(process.env.PORT) || 8080)
.catch((e) => {
console.error(e);
process.exit(1);
});
console.log('App is running in port: ' + process.env.PORT);
}
catch (error) {
console.log("Error message: " + error);
}
}
bootstrap().catch(e => {
console.error(e);
});
//# sourceMappingURL=main.js.map
解决方法
正如张小龙在评论中所建议的那样,我将发布自己找到的解决方案。
所以,错误的原因确实很愚蠢。
1。)使用Plesk控制面板(在IIS服务器中),无论将NestJs应用程序部署到站点的根目录还是子文件夹(如默认的dist文件夹),获取路径设置以引用项目都是至关重要的正确地:
现在,在上图中,项目的路径设置在部署到子文件夹(即默认的构建文件夹dist)时是正确的。我的错误是应用程序启动文件的路径引用为“ /dist/main.js”,因为它应该只是main.js。
此简单更正应用程序正常后,使用默认的IIS端口(端口80和SSL保护的连接443)正常启动。 main.ts -file除了以下内容外,不需要任何其他内容,其中IIS默认端口是通过process.env.PORT自动获取的:
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(process.env.PORT);
}
bootstrap();
2。)必须正确配置的第二个简单的东西是web.config(在我看来,这本来是正确的):
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="iisnode" path="dist/main.js" verb="*" modules="iisnode" />
</handlers>
<iisnode nodeProcessCommandLine="C:\Program Files\nodejs\node.exe">
</iisnode>
<rewrite>
<rules>
<rule name="nestjs">
<match url="/*" />
<action type="Rewrite" url="main.js" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
我希望这可以帮助那些可能对Plesk中的IISnode设置感到困惑的人。这些设置通常适用于所有Node.js应用程序,它们不仅限于NestJ,后者只是node.js的上层框架,尤其是Express上的框架。但是IISNode中的错误消息有时可能会引起误解,并且很难看清它们。
NestJs真的很棒,而且非常感谢那些实施IISnode的人,那些必须或喜欢使用IIS的人可以轻松地将超现代的NestJs后端应用程序部署到IIS服务器。