From 33c2e787f45848b0d3357c3100172c14d2aa103a Mon Sep 17 00:00:00 2001 From: GiveMe-A-Name Date: Thu, 25 Jul 2024 14:26:01 +0800 Subject: [PATCH] fix: `createDevServer` should not listen port by default --- .changeset/strong-moose-wash.md | 7 ++++++ packages/server/server/src/createDevServer.ts | 12 ---------- .../solutions/app-tools/src/commands/dev.ts | 24 +++++++++++++++---- 3 files changed, 27 insertions(+), 16 deletions(-) create mode 100644 .changeset/strong-moose-wash.md diff --git a/.changeset/strong-moose-wash.md b/.changeset/strong-moose-wash.md new file mode 100644 index 000000000000..d33735ff7183 --- /dev/null +++ b/.changeset/strong-moose-wash.md @@ -0,0 +1,7 @@ +--- +'@modern-js/app-tools': patch +'@modern-js/server': patch +--- + +fix: `createDevServer` should not listen port by default +fix: `createDevServer` 默认不应该监听端口 diff --git a/packages/server/server/src/createDevServer.ts b/packages/server/server/src/createDevServer.ts index 81f80a7e1e95..4048378fb0a0 100644 --- a/packages/server/server/src/createDevServer.ts +++ b/packages/server/server/src/createDevServer.ts @@ -53,17 +53,5 @@ export async function createDevServer( await server.init(); - nodeServer.listen( - { - host: dev.host || '127.0.0.1', - port: dev.port || '8080', - }, - (err?: Error) => { - if (err) { - throw err; - } - }, - ); - return nodeServer; } diff --git a/packages/solutions/app-tools/src/commands/dev.ts b/packages/solutions/app-tools/src/commands/dev.ts index 9da3c9920e94..e26a805ca525 100644 --- a/packages/solutions/app-tools/src/commands/dev.ts +++ b/packages/solutions/app-tools/src/commands/dev.ts @@ -1,6 +1,11 @@ import path from 'node:path'; import { PluginAPI, ResolvedConfigContext } from '@modern-js/core'; -import { DEFAULT_DEV_HOST, SERVER_DIR, getMeta } from '@modern-js/utils'; +import { + DEFAULT_DEV_HOST, + SERVER_DIR, + getMeta, + logger, +} from '@modern-js/utils'; import { createDevServer } from '@modern-js/server'; import { applyPlugins } from '@modern-js/prod-server'; import { loadServerPlugins } from '../utils/loadPlugins'; @@ -97,6 +102,8 @@ export const dev = async ( ...devServerOptions, }; + const host = normalizedConfig.dev?.host || DEFAULT_DEV_HOST; + if (apiOnly) { const server = await createDevServer( { @@ -106,8 +113,6 @@ export const dev = async ( applyPlugins, ); - const host = normalizedConfig.dev?.host || DEFAULT_DEV_HOST; - server.listen( { port, @@ -126,7 +131,18 @@ export const dev = async ( applyPlugins, ); - // TODO: set correct server + server.listen( + { + port, + host, + }, + (err?: Error) => { + if (err) { + logger.error('Occur error %s, when start dev server', err); + } + }, + ); + setServer(server); } };