diff --git a/docs/docs/cli-commands.md b/docs/docs/cli-commands.md index 3851a7d80b17..239b7018b925 100644 --- a/docs/docs/cli-commands.md +++ b/docs/docs/cli-commands.md @@ -1993,7 +1993,6 @@ yarn redwood serve [side] | ------------------- | ------------------------------------------------------------------------------ | | `side` | Which side(s) to run. Choices are `api` and `web`. Defaults to `api` and `web` | | `--port` | What port should the server run on [default: 8911] | -| `--host` | What host should the server run on. This defaults to the value of `web.host` in the `redwood.toml` file which itself defaults to `'localhost'`. | | `--socket` | The socket the server should run. This takes precedence over port | ### serve api @@ -2009,7 +2008,6 @@ This command uses `apiUrl` in your `redwood.toml`. Use this command if you want | Arguments & Options | Description | | ------------------- | ----------------------------------------------------------------- | | `--port` | What port should the server run on [default: 8911] | -| `--host` | What host should the server run on. This defaults to the value of `api.host` in the `redwood.toml` file which itself defaults to `'localhost'`. | | `--socket` | The socket the server should run. This takes precedence over port | | `--apiRootPath` | The root path where your api functions are served | @@ -2037,7 +2035,6 @@ This command serves the contents in `web/dist`. Use this command if you're debug | Arguments & Options | Description | | ------------------- | ------------------------------------------------------------------------------------- | | `--port` | What port should the server run on [default: 8911] | -| `--host` | What host should the server run on. This defaults to the value of `web.host` in the `redwood.toml` file which itself defaults to `'localhost'`. | | `--socket` | The socket the server should run. This takes precedence over port | | `--apiHost` | Forwards requests from the `apiUrl` (defined in `redwood.toml`) to the specified host | diff --git a/packages/api-server/src/cliHandlers.ts b/packages/api-server/src/cliHandlers.ts index bb6060ff5e43..0f96c68099a4 100644 --- a/packages/api-server/src/cliHandlers.ts +++ b/packages/api-server/src/cliHandlers.ts @@ -18,33 +18,13 @@ const sendProcessReady = () => { return process.send && process.send('ready') } -const redwoodProjectConfig = getConfig() - export const commonOptions = { - port: { - default: redwoodProjectConfig.web.port, - type: 'number', - alias: 'p', - }, - host: { - default: redwoodProjectConfig.web.host, - type: 'string', - alias: 'h', - }, + port: { default: getConfig().web?.port || 8910, type: 'number', alias: 'p' }, socket: { type: 'string' }, } as const export const apiCliOptions = { - port: { - default: redwoodProjectConfig.api.port, - type: 'number', - alias: 'p', - }, - host: { - default: redwoodProjectConfig.api.host, - type: 'string', - alias: 'h', - }, + port: { default: getConfig().api?.port || 8911, type: 'number', alias: 'p' }, socket: { type: 'string' }, apiRootPath: { alias: ['rootPath', 'root-path'], @@ -56,16 +36,7 @@ export const apiCliOptions = { } as const export const webCliOptions = { - port: { - default: redwoodProjectConfig.web.port, - type: 'number', - alias: 'p', - }, - host: { - default: redwoodProjectConfig.web.host, - type: 'string', - alias: 'h', - }, + port: { default: getConfig().web?.port || 8910, type: 'number', alias: 'p' }, socket: { type: 'string' }, apiHost: { alias: 'api-host', @@ -75,7 +46,7 @@ export const webCliOptions = { } as const export const apiServerHandler = async (options: ApiServerArgs) => { - const { port, host, socket, apiRootPath } = options + const { port, socket, apiRootPath } = options const tsApiServer = Date.now() process.stdout.write(c.dim(c.italic('Starting API Server...\n'))) @@ -86,7 +57,6 @@ export const apiServerHandler = async (options: ApiServerArgs) => { const http = startFastifyServer({ port, - host, socket, fastify, }).ready(() => { @@ -94,7 +64,7 @@ export const apiServerHandler = async (options: ApiServerArgs) => { const on = socket ? socket - : c.magenta(`http://${host}:${port}${apiRootPath}`) + : c.magenta(`http://localhost:${port}${apiRootPath}`) console.log(`API listening on ${on}`) const graphqlEnd = c.magenta(`${apiRootPath}graphql`) console.log(`GraphQL endpoint at ${graphqlEnd}`) @@ -106,10 +76,10 @@ export const apiServerHandler = async (options: ApiServerArgs) => { } export const bothServerHandler = async (options: BothServerArgs) => { - const { port, host, socket } = options + const { port, socket } = options const tsServer = Date.now() process.stdout.write(c.dim(c.italic('Starting API and Web Servers...\n'))) - const apiRootPath = coerceRootPath(redwoodProjectConfig.web.apiUrl) + const apiRootPath = coerceRootPath(getConfig().web.apiUrl) let fastify = createFastifyInstance() @@ -119,16 +89,15 @@ export const bothServerHandler = async (options: BothServerArgs) => { startFastifyServer({ port, - host, socket, fastify, }).ready(() => { console.log(c.italic(c.dim('Took ' + (Date.now() - tsServer) + ' ms'))) const on = socket ? socket - : c.magenta(`http://${host}:${port}${apiRootPath}`) - const webServer = c.green(`http://${host}:${port}`) - const apiServer = c.magenta(`http://${host}:${port}`) + : c.magenta(`http://localhost:${port}${apiRootPath}`) + const webServer = c.green(`http://localhost:${port}`) + const apiServer = c.magenta(`http://localhost:${port}`) console.log(`Web server started on ${webServer}`) console.log(`API serving from ${apiServer}`) console.log(`API listening on ${on}`) @@ -139,14 +108,14 @@ export const bothServerHandler = async (options: BothServerArgs) => { } export const webServerHandler = async (options: WebServerArgs) => { - const { port, host, socket, apiHost } = options + const { port, socket, apiHost } = options const tsServer = Date.now() process.stdout.write(c.dim(c.italic('Starting Web Server...\n'))) - const apiUrl = redwoodProjectConfig.web.apiUrl + const apiUrl = getConfig().web.apiUrl // Construct the graphql url from apiUrl by default // But if apiGraphQLUrl is specified, use that instead const graphqlEndpoint = coerceRootPath( - redwoodProjectConfig.web.apiGraphQLUrl ?? `${apiUrl}/graphql` + getConfig().web.apiGraphQLUrl ?? `${apiUrl}/graphql` ) let fastify = createFastifyInstance() @@ -162,8 +131,7 @@ export const webServerHandler = async (options: WebServerArgs) => { } startFastifyServer({ - port, - host, + port: port, socket, fastify, }).ready(() => { @@ -171,7 +139,7 @@ export const webServerHandler = async (options: WebServerArgs) => { if (socket) { console.log(`Listening on ` + c.magenta(`${socket}`)) } - const webServer = c.green(`http://${host}:${port}`) + const webServer = c.green(`http://localhost:${port}`) console.log(`Web server started on ${webServer}`) console.log(`GraphQL endpoint is set to ` + c.magenta(`${graphqlEndpoint}`)) sendProcessReady() diff --git a/packages/api-server/src/server.ts b/packages/api-server/src/server.ts index 8aed3d6c610c..169884f36e37 100644 --- a/packages/api-server/src/server.ts +++ b/packages/api-server/src/server.ts @@ -2,17 +2,16 @@ import { FastifyInstance } from 'fastify' export interface HttpServerParams { port: number - host?: string socket?: string fastify: FastifyInstance } export const startServer = ({ port = 8911, - host = 'localhost', socket, fastify, }: HttpServerParams) => { + const host = 'localhost' const serverPort = socket ? parseInt(socket) : port fastify.listen({ port: serverPort, host }) diff --git a/packages/api-server/src/watch.ts b/packages/api-server/src/watch.ts index 3fc4e5bc15a3..02b7dda517f2 100644 --- a/packages/api-server/src/watch.ts +++ b/packages/api-server/src/watch.ts @@ -1,5 +1,4 @@ #!/usr/bin/env node -// This script is called by the `yarn rw dev` command. Specifically, it's the api command. import { fork } from 'child_process' import type { ChildProcess } from 'child_process' @@ -17,9 +16,6 @@ import { buildApi } from '@redwoodjs/internal/dist/build/api' import { loadAndValidateSdls } from '@redwoodjs/internal/dist/validateSchema' import { getPaths, ensurePosixPath, getConfig } from '@redwoodjs/project-config' -const redwoodProjectPaths = getPaths() -const redwoodProjectConfig = getConfig() - const argv = yargs(hideBin(process.argv)) .option('debug-port', { alias: 'dp', @@ -30,19 +26,15 @@ const argv = yargs(hideBin(process.argv)) alias: 'p', description: 'Port', type: 'number', - default: redwoodProjectConfig.api.port, - }) - .option('host', { - alias: 'h', - description: 'Host', - type: 'string', - default: redwoodProjectConfig.api.host, }) + .help() + .alias('help', 'h') .parseSync() -// If this is run via the yarn rw dev command, this will have already been called. +const rwjsPaths = getPaths() + dotenv.config({ - path: redwoodProjectPaths.base, + path: rwjsPaths.base, }) // TODO: @@ -85,9 +77,9 @@ const rebuildApiServer = () => { } // OpenTelemetry SDK Setup - if (redwoodProjectConfig.experimental.opentelemetry.enabled) { + if (getConfig().experimental.opentelemetry.enabled) { const opentelemetrySDKScriptPath = - redwoodProjectConfig.experimental.opentelemetry.apiSdk + getConfig().experimental.opentelemetry.apiSdk if (opentelemetrySDKScriptPath) { console.log( `Setting up OpenTelemetry using the setup file: ${opentelemetrySDKScriptPath}` @@ -109,10 +101,12 @@ const rebuildApiServer = () => { forkOpts.execArgv = forkOpts.execArgv.concat([`--inspect=${debugPort}`]) } + const port = argv.port ?? getConfig().api.port + // Start API server httpServerProcess = fork( path.join(__dirname, 'index.js'), - ['api', '--port', argv.port.toString(), '--host', `${argv.host}`], + ['api', '--port', port.toString()], forkOpts ) } catch (e) { @@ -132,16 +126,16 @@ const delayRestartServer = debounce( ) // NOTE: the file comes through as a unix path, even on windows -// So we need to convert the redwoodProjectPaths +// So we need to convert the rwjsPaths const IGNORED_API_PATHS = [ - 'api/dist', // use this, because using redwoodProjectPaths.api.dist seems to not ignore on first build - redwoodProjectPaths.api.types, - redwoodProjectPaths.api.db, + 'api/dist', // use this, because using rwjsPaths.api.dist seems to not ignore on first build + rwjsPaths.api.types, + rwjsPaths.api.db, ].map((path) => ensurePosixPath(path)) chokidar - .watch(redwoodProjectPaths.api.base, { + .watch(rwjsPaths.api.base, { persistent: true, ignoreInitial: true, ignored: (file: string) => { @@ -180,9 +174,7 @@ chokidar } console.log( - c.dim( - `[${eventName}] ${filePath.replace(redwoodProjectPaths.api.base, '')}` - ) + c.dim(`[${eventName}] ${filePath.replace(rwjsPaths.api.base, '')}`) ) delayRestartServer.cancel() delayRestartServer() diff --git a/packages/cli/src/commands/__tests__/dev.test.js b/packages/cli/src/commands/__tests__/dev.test.js index 5b463dfb0117..f7ab0d7f80e2 100644 --- a/packages/cli/src/commands/__tests__/dev.test.js +++ b/packages/cli/src/commands/__tests__/dev.test.js @@ -76,7 +76,6 @@ describe('yarn rw dev', () => { getConfig.mockReturnValue({ web: { port: 8910, - host: 'localhost', }, api: { port: 8911, @@ -101,7 +100,7 @@ describe('yarn rw dev', () => { ) expect(apiCommand.command).toMatchInlineSnapshot( - `"yarn cross-env NODE_ENV=development NODE_OPTIONS=--enable-source-maps yarn nodemon --quiet --watch "/mocked/project/redwood.toml" --exec "yarn rw-api-server-watch --port 8911 --host '::' --debug-port 18911 | rw-log-formatter""` + `"yarn cross-env NODE_ENV=development NODE_OPTIONS=--enable-source-maps yarn nodemon --quiet --watch "/mocked/project/redwood.toml" --exec "yarn rw-api-server-watch --port 8911 --debug-port 18911 | rw-log-formatter""` ) expect(generateCommand.command).toEqual('yarn rw-gen-watch') @@ -111,7 +110,6 @@ describe('yarn rw dev', () => { getConfig.mockReturnValue({ web: { port: 8910, - host: 'localhost', }, api: { port: 8911, @@ -129,7 +127,7 @@ describe('yarn rw dev', () => { const apiCommand = find(concurrentlyArgs, { name: 'api' }) expect(apiCommand.command).toContain( - "yarn rw-api-server-watch --port 8911 --host '::' --debug-port 90909090" + 'yarn rw-api-server-watch --port 8911 --debug-port 90909090' ) }) @@ -137,7 +135,6 @@ describe('yarn rw dev', () => { getConfig.mockReturnValue({ web: { port: 8910, - host: 'localhost', }, api: { port: 8911, @@ -160,7 +157,6 @@ describe('yarn rw dev', () => { getConfig.mockReturnValue({ web: { port: 8910, - host: 'localhost', bundler: 'vite', // <-- enable vite mode }, api: { diff --git a/packages/cli/src/commands/__tests__/serve.test.js b/packages/cli/src/commands/__tests__/serve.test.js index 458e759d4009..35d4aeda3745 100644 --- a/packages/cli/src/commands/__tests__/serve.test.js +++ b/packages/cli/src/commands/__tests__/serve.test.js @@ -17,12 +17,7 @@ jest.mock('@redwoodjs/project-config', () => { }, getConfig: () => { return { - web: { - host: 'localhost', - }, - api: { - host: 'localhost', - }, + api: {}, } }, } @@ -72,7 +67,6 @@ describe('yarn rw serve', () => { expect(apiServerHandler).toHaveBeenCalledWith( expect.objectContaining({ port: 5555, - host: 'localhost', apiRootPath: expect.stringMatching(/^\/?funkyFunctions\/?$/), }) ) @@ -88,7 +82,6 @@ describe('yarn rw serve', () => { expect(apiServerHandler).toHaveBeenCalledWith( expect.objectContaining({ port: 5555, - host: 'localhost', rootPath: expect.stringMatching(/^\/?funkyFunctions\/nested\/$/), }) ) @@ -104,7 +97,6 @@ describe('yarn rw serve', () => { expect(webServerHandler).toHaveBeenCalledWith( expect.objectContaining({ port: 9898, - host: 'localhost', socket: 'abc', apiHost: 'https://myapi.redwood/api', }) @@ -119,7 +111,6 @@ describe('yarn rw serve', () => { expect(bothServerHandler).toHaveBeenCalledWith( expect.objectContaining({ port: 9898, - host: 'localhost', socket: 'abc', }) ) diff --git a/packages/cli/src/commands/devHandler.js b/packages/cli/src/commands/devHandler.js index 0bebe99aa04f..31ff262545a9 100644 --- a/packages/cli/src/commands/devHandler.js +++ b/packages/cli/src/commands/devHandler.js @@ -21,12 +21,11 @@ export const handler = async ({ watchNodeModules = process.env.RWJS_WATCH_NODE_MODULES === '1', apiDebugPort, }) => { - const redwoodProjectPaths = getPaths() - const redwoodProjectConfig = getConfig() + const rwjsPaths = getPaths() // Starting values of ports from config (redwood.toml) - let apiPreferredPort = parseInt(redwoodProjectConfig.api.port) - let webPreferredPort = parseInt(redwoodProjectConfig.web.port) + let apiPreferredPort = parseInt(getConfig().api.port) + let webPreferredPort = parseInt(getConfig().web.port) // Assume we can have the ports we want let apiAvailablePort = apiPreferredPort @@ -85,7 +84,7 @@ export const handler = async ({ await generatePrismaClient({ verbose: false, force: false, - schema: redwoodProjectPaths.api.dbSchema, + schema: rwjsPaths.api.dbSchema, }) } catch (e) { errorTelemetry( @@ -128,7 +127,7 @@ export const handler = async ({ return `--debug-port ${defaultApiDebugPort}` } - const apiDebugPortInToml = redwoodProjectConfig.api.debugPort + const apiDebugPortInToml = getConfig().api.debugPort if (apiDebugPortInToml) { return `--debug-port ${apiDebugPortInToml}` } @@ -140,47 +139,26 @@ export const handler = async ({ const redwoodConfigPath = getConfigPath() const webCommand = - redwoodProjectConfig.web.bundler === 'vite' // @NOTE: can't use enums, not TS + getConfig().web.bundler === 'vite' // @NOTE: can't use enums, not TS ? `yarn cross-env NODE_ENV=development rw-vite-dev` : `yarn cross-env NODE_ENV=development RWJS_WATCH_NODE_MODULES=${ watchNodeModules ? '1' : '' } webpack serve --config "${webpackDevConfig}" ${forward}` - const apiCommand = [ - 'yarn', - 'cross-env', - 'NODE_ENV=development', - 'NODE_OPTIONS=--enable-source-maps', - 'yarn', - 'nodemon', - '--quiet', - `--watch "${redwoodConfigPath}"`, - '--exec', - `"${[ - 'yarn', - 'rw-api-server-watch', - `--port ${apiAvailablePort}`, - `--host '::'`, - getApiDebugFlag(), - '|', - 'rw-log-formatter', - ].join(' ')}"`, - ].join(' ') - /** @type {Record} */ const jobs = { api: { name: 'api', - command: apiCommand, + command: `yarn cross-env NODE_ENV=development NODE_OPTIONS=--enable-source-maps yarn nodemon --quiet --watch "${redwoodConfigPath}" --exec "yarn rw-api-server-watch --port ${apiAvailablePort} ${getApiDebugFlag()} | rw-log-formatter"`, prefixColor: 'cyan', - runWhen: () => fs.existsSync(redwoodProjectPaths.api.src), + runWhen: () => fs.existsSync(rwjsPaths.api.src), }, web: { name: 'web', command: webCommand, prefixColor: 'blue', - cwd: redwoodProjectPaths.web.base, - runWhen: () => fs.existsSync(redwoodProjectPaths.web.src), + cwd: rwjsPaths.web.base, + runWhen: () => fs.existsSync(rwjsPaths.web.src), }, gen: { name: 'gen', diff --git a/packages/cli/src/commands/serve.js b/packages/cli/src/commands/serve.js index a76014f8e0b5..4267df3ea758 100644 --- a/packages/cli/src/commands/serve.js +++ b/packages/cli/src/commands/serve.js @@ -11,34 +11,14 @@ import c from '../lib/colors' export const command = 'serve [side]' export const description = 'Run server for api or web in production' -export async function builder(yargs) { - const redwoodProjectPaths = getPaths() - const redwoodProjectConfig = getConfig() - +export const builder = async (yargs) => { yargs .usage('usage: $0 ') .command({ command: '$0', - description: 'Run both api and web servers. Uses the web port and host', - builder: (yargs) => - yargs.options({ - port: { - default: redwoodProjectConfig.web.port, - type: 'number', - alias: 'p', - }, - host: { - default: redwoodProjectConfig.web.host, - type: 'string', - alias: 'h', - }, - socket: { type: 'string' }, - }), + descriptions: 'Run both api and web servers', handler: async (argv) => { - const serverFilePath = path.join( - redwoodProjectPaths.api.dist, - 'server.js' - ) + const serverFilePath = path.join(getPaths().api.dist, 'server.js') if (fs.existsSync(serverFilePath)) { console.log( @@ -52,7 +32,7 @@ export async function builder(yargs) { ) await execa('yarn', ['node', path.join('dist', 'server.js')], { - cwd: redwoodProjectPaths.api.base, + cwd: getPaths().api.base, stdio: 'inherit', shell: true, }) @@ -63,51 +43,54 @@ export async function builder(yargs) { const { bothServerHandler } = await import('./serveHandler.js') await bothServerHandler(argv) }, + builder: (yargs) => + yargs.options({ + port: { + default: getConfig().web?.port || 8910, + type: 'number', + alias: 'p', + }, + socket: { type: 'string' }, + }), }) .command({ command: 'api', - description: 'Start server for serving only the api', + description: 'start server for serving only the api', + handler: async (argv) => { + const { apiServerHandler } = await import('./serveHandler.js') + await apiServerHandler(argv) + }, builder: (yargs) => yargs.options({ port: { - default: redwoodProjectConfig.api.port, + default: getConfig().api?.port || 8911, type: 'number', alias: 'p', }, - host: { - default: redwoodProjectConfig.api.host, - type: 'string', - alias: 'h', - }, socket: { type: 'string' }, apiRootPath: { - alias: ['api-root-path', 'rootPath', 'root-path'], + alias: ['rootPath', 'root-path'], default: '/', type: 'string', desc: 'Root path where your api functions are served', coerce: coerceRootPath, }, }), - handler: async (argv) => { - const { apiServerHandler } = await import('./serveHandler.js') - await apiServerHandler(argv) - }, }) .command({ command: 'web', - description: 'Start server for serving only the web side', + description: 'start server for serving only the web side', + handler: async (argv) => { + const { webServerHandler } = await import('./serveHandler.js') + await webServerHandler(argv) + }, builder: (yargs) => yargs.options({ port: { - default: redwoodProjectConfig.web.port, + default: getConfig().web?.port || 8910, type: 'number', alias: 'p', }, - host: { - default: redwoodProjectConfig.web.host, - type: 'string', - alias: 'h', - }, socket: { type: 'string' }, apiHost: { alias: 'api-host', @@ -115,10 +98,6 @@ export async function builder(yargs) { desc: 'Forward requests from the apiUrl, defined in redwood.toml to this host', }, }), - handler: async (argv) => { - const { webServerHandler } = await import('./serveHandler.js') - await webServerHandler(argv) - }, }) .middleware((argv) => { // Make sure the relevant side has been built, before serving @@ -126,7 +105,7 @@ export async function builder(yargs) { if ( positionalArgs.includes('web') && - !fs.existsSync(path.join(redwoodProjectPaths.web.dist), 'index.html') + !fs.existsSync(path.join(getPaths().web.dist), 'index.html') ) { console.error( c.error( @@ -138,7 +117,7 @@ export async function builder(yargs) { if ( positionalArgs.includes('api') && - !fs.existsSync(path.join(redwoodProjectPaths.api.dist)) + !fs.existsSync(path.join(getPaths().api.dist)) ) { console.error( c.error( @@ -151,8 +130,8 @@ export async function builder(yargs) { if ( // serve both positionalArgs.length === 1 && - (!fs.existsSync(path.join(redwoodProjectPaths.api.dist)) || - !fs.existsSync(path.join(redwoodProjectPaths.web.dist), 'index.html')) + (!fs.existsSync(path.join(getPaths().api.dist)) || + !fs.existsSync(path.join(getPaths().web.dist), 'index.html')) ) { console.error( c.error( @@ -179,9 +158,6 @@ const separator = chalk.hex('#ff845e')( '------------------------------------------------------------------' ) -// We'll clean this up later, but for now note that this function is -// duplicated between this package and @redwoodjs/fastify -// to avoid importing @redwoodjs/fastify when the CLI starts. export function coerceRootPath(path) { // Make sure that we create a root path that starts and ends with a slash (/) const prefix = path.charAt(0) !== '/' ? '/' : '' diff --git a/packages/cli/src/commands/serveHandler.js b/packages/cli/src/commands/serveHandler.js index 1c6b4af016b7..2f5aae4a2ca8 100644 --- a/packages/cli/src/commands/serveHandler.js +++ b/packages/cli/src/commands/serveHandler.js @@ -10,7 +10,7 @@ import { withApiProxy } from '@redwoodjs/fastify/dist/plugins/withApiProxy' import { getConfig } from '@redwoodjs/project-config' export const apiServerHandler = async (options) => { - const { port, host, socket, apiRootPath } = options + const { port, socket, apiRootPath } = options const tsApiServer = Date.now() console.log(chalk.dim.italic('Starting API Server...')) @@ -29,7 +29,7 @@ export const apiServerHandler = async (options) => { fastify.listen({ port: socket ? parseInt(socket) : port, - host, + host: process.env.NODE_ENV === 'production' ? '0.0.0.0' : '::', }) fastify.ready(() => { @@ -42,7 +42,7 @@ export const apiServerHandler = async (options) => { const on = socket ? socket - : chalk.magenta(`http://${host}:${port}${apiRootPath}`) + : chalk.magenta(`http://localhost:${port}${apiRootPath}`) console.log(`API listening on ${on}`) const graphqlEnd = chalk.magenta(`${apiRootPath}graphql`) @@ -53,7 +53,7 @@ export const apiServerHandler = async (options) => { } export const bothServerHandler = async (options) => { - const { port, host, socket } = options + const { port, socket } = options const tsServer = Date.now() console.log(chalk.italic.dim('Starting API and Web Servers...')) @@ -81,7 +81,7 @@ export const bothServerHandler = async (options) => { fastify.listen({ port: socket ? parseInt(socket) : port, - host, + host: process.env.NODE_ENV === 'production' ? '0.0.0.0' : '::', }) fastify.ready(() => { @@ -89,10 +89,10 @@ export const bothServerHandler = async (options) => { const on = socket ? socket - : chalk.magenta(`http://${host}:${port}${apiRootPath}`) + : chalk.magenta(`http://localhost:${port}${apiRootPath}`) - const webServer = chalk.green(`http://${host}:${port}`) - const apiServer = chalk.magenta(`http://${host}:${port}`) + const webServer = chalk.green(`http://localhost:${port}`) + const apiServer = chalk.magenta(`http://localhost:${port}`) console.log(`Web server started on ${webServer}`) console.log(`API serving from ${apiServer}`) console.log(`API listening on ${on}`) @@ -104,17 +104,16 @@ export const bothServerHandler = async (options) => { } export const webServerHandler = async (options) => { - const redwoodProjectConfig = getConfig() - const { port, host, socket, apiHost } = options + const { port, socket, apiHost } = options const tsServer = Date.now() console.log(chalk.dim.italic('Starting Web Server...')) - const apiUrl = redwoodProjectConfig.web.apiUrl + const apiUrl = getConfig().web.apiUrl // Construct the GraphQL URL from apiUrl by default. // But if apiGraphQLUrl is specified, use that instead. const graphqlEndpoint = coerceRootPath( - redwoodProjectConfig.web.apiGraphQLUrl ?? `${apiUrl}/graphql` + getConfig().web.apiGraphQLUrl ?? `${apiUrl}/graphql` ) const fastify = createFastifyInstance() @@ -139,7 +138,7 @@ export const webServerHandler = async (options) => { fastify.listen({ port: socket ? parseInt(socket) : port, - host, + host: process.env.NODE_ENV === 'production' ? '0.0.0.0' : '::', }) fastify.ready(() => { @@ -149,7 +148,7 @@ export const webServerHandler = async (options) => { console.log(`Listening on ` + chalk.magenta(`${socket}`)) } - const webServer = chalk.green(`http://${host}:${port}`) + const webServer = chalk.green(`http://localhost:${port}`) console.log(`Web server started on ${webServer}`) console.log( `GraphQL endpoint is set to ` + chalk.magenta(`${graphqlEndpoint}`) diff --git a/packages/core/config/webpack.development.js b/packages/core/config/webpack.development.js index 3bb07f275435..e51cea0322b8 100644 --- a/packages/core/config/webpack.development.js +++ b/packages/core/config/webpack.development.js @@ -98,7 +98,7 @@ const baseConfig = merge(webpackConfig('development'), { historyApiFallback: { disableDotRule: true, }, - host: redwoodConfig.web.host, + host: redwoodConfig.web.host || 'localhost', port: redwoodConfig.web.port, proxy: getProxyConfig(), open: redwoodConfig.browser.open,