diff --git a/README.md b/README.md index 41a59bc4..7e668ec9 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,15 @@ Example: } ``` +### internalServer + +- Type: `Boolean` +- Default: `false` + +If you have defined `serverMiddleware` in your `nuxt.config`, this builder will automatically enable an internal server within the lambda so you can access your own endpoints via `http://localhost:3000`. (This does not affect how you call your endpoints from client-side.) + +If you need to enable or disable the internal server manually (for example, if you are adding server middleware via a module), just set `internalServer` within the builder options. + ### `generateStaticRoutes` - Type: `Boolean` diff --git a/src/build.ts b/src/build.ts index d628922a..d1d7b220 100644 --- a/src/build.ts +++ b/src/build.ts @@ -24,6 +24,7 @@ interface NuxtBuilderConfig { generateStaticRoutes?: boolean includeFiles?: string[] | string serverFiles?: string[] + internalServer?: boolean } export async function build (opts: BuildOptions & { config: NuxtBuilderConfig }): Promise { @@ -137,6 +138,7 @@ export async function build (opts: BuildOptions & { config: NuxtBuilderConfig }) const buildDir = nuxtConfigFile.buildDir ? path.relative(entrypointPath, nuxtConfigFile.buildDir) : '.nuxt' const srcDir = nuxtConfigFile.srcDir ? path.relative(entrypointPath, nuxtConfigFile.srcDir) : '.' const lambdaName = nuxtConfigFile.lambdaName ? nuxtConfigFile.lambdaName : 'index' + const usesServerMiddleware = config.internalServer !== undefined ? config.internalServer : !!nuxtConfigFile.serverMiddleware await exec('nuxt', [ 'build', @@ -222,6 +224,7 @@ export async function build (opts: BuildOptions & { config: NuxtBuilderConfig }) const launcherSrc = (await fs.readFile(launcherPath, 'utf8')) .replace(/__NUXT_SUFFIX__/g, nuxtDep.suffix) .replace(/__NUXT_CONFIG__/g, './' + nuxtConfigName) + .replace(/\/\* __ENABLE_INTERNAL_SERVER__ \*\/ *true/g, String(usesServerMiddleware)) const launcherFiles = { 'vercel__launcher.js': new FileBlob({ data: launcherSrc }), diff --git a/src/launcher.ts b/src/launcher.ts index 06545bda..9b9f1ca3 100644 --- a/src/launcher.ts +++ b/src/launcher.ts @@ -1,4 +1,4 @@ -import http from 'http' +import type { RequestListener } from 'http' import esmCompiler from 'esm' const startTime = process.hrtime() @@ -33,19 +33,26 @@ const readyPromise = nuxt.ready().then(() => { }) // Create bridge and start listening -const { Server } = require('http') as typeof http // eslint-disable-line import/order +const { Server } = require('http') as typeof import('http') // eslint-disable-line import/order const { Bridge } = require('./vercel__bridge.js') as typeof import('@vercel/node-bridge/bridge') -const server = new Server( - async (req, res) => { - if (!isReady) { - await readyPromise - } - nuxt.server.app(req, res) +const requestListener: RequestListener = async (req, res) => { + if (!isReady) { + await readyPromise } -) -const bridge = new Bridge(server) + nuxt.server.app(req, res) +} +// This is used by Vercel +const server = new Server(requestListener) +const bridge = new Bridge(server) bridge.listen() +// eslint-disable-next-line +if (/* __ENABLE_INTERNAL_SERVER__ */true) { + // Allow internal calls from Nuxt to endpoints registered as serverMiddleware + const internalServer = new Server(requestListener) + internalServer.listen(3000, '127.0.0.1') +} + export const launcher: typeof bridge.launcher = bridge.launcher