/server folder

Hi! I've just migrated my app from Nuxt 2 to Nuxt 3. Lots of things seems to work well, apart from auto registering some server logic. /plugins/ignoreRouterWarnings.server.ts is not run on the server /server/middleware/test.ts is not run on the server I'd be happy to explicitly configure this instead of relying on file conventions but when I tried serverHandlers in nuxt config that completely borked my local server, the server became unknown host I'm trying to run the simplest logic in /server/middleware/test.ts :
import { defineEventHandler, getRequestURL } from 'h3';

export default defineEventHandler((event) => {
console.log('Test middleware triggered');
console.log('Request URL:', getRequestURL(event).pathname);
});
import { defineEventHandler, getRequestURL } from 'h3';

export default defineEventHandler((event) => {
console.log('Test middleware triggered');
console.log('Request URL:', getRequestURL(event).pathname);
});
Nothing gets logged in CLI. SSR otherwise is confirmed to work. I'm at "nuxt": "3.15.4" My nitro nuxt config is
nitro: {
// devProxy: {
// '/api': {
// target: process.env.API_HOST || 'https://goout.net',
// changeOrigin: true,
// rewrite: (path) => path.replace(/^\/api/, ''),
// },
// },
logLevel: 5,
compatibilityDate: '2025-03-13',
},
nitro: {
// devProxy: {
// '/api': {
// target: process.env.API_HOST || 'https://goout.net',
// changeOrigin: true,
// rewrite: (path) => path.replace(/^\/api/, ''),
// },
// },
logLevel: 5,
compatibilityDate: '2025-03-13',
},
I tried commenting out the devProxy thinking it might interfere, but it's not it. Any tips welcome I've been stuck on this for hours 🙏️
8 Replies
kapa.ai
kapa.ai2mo ago
Hi @Martin Malinda. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- I understand you're having issues with server middleware and server plugins not being automatically registered in your Nuxt 3 migration. Let's troubleshoot this issue. Server Middleware Registration In Nuxt 3, files in the ~/server/middleware directory should be automatically registered and run on every request before any other server routes. Your middleware looks correctly structured:
import { defineEventHandler, getRequestURL } from 'h3';

export default defineEventHandler((event) => {
console.log('Test middleware triggered');
console.log('Request URL:', getRequestURL(event).pathname);
});
import { defineEventHandler, getRequestURL } from 'h3';

export default defineEventHandler((event) => {
console.log('Test middleware triggered');
console.log('Request URL:', getRequestURL(event).pathname);
});
Possible Solutions 1. Check Server Directory Structure: Make sure your server middleware is in the correct location. In Nuxt 3, the structure should be:
-| server/
---| middleware/
-----| test.ts
-| server/
---| middleware/
-----| test.ts
2. Server Types Configuration: Since Nuxt 3.5+, you might need to add a tsconfig.json file in your server directory to improve type checking:
{
"extends": "../.nuxt/tsconfig.server.json"
}
{
"extends": "../.nuxt/tsconfig.server.json"
}
This can help with IDE integration Server Types. 3. Plugin Naming Convention: For server plugins, ensure they follow the correct naming convention with .server.ts suffix and are placed in the plugins/ directory. 4. Nitro Configuration: Your Nitro configuration looks fine, but the logLevel: 5 might be too verbose. Try setting it to a lower value like 3 for better readability. 5. Check for Runtime Modules: In Nuxt 3, modules shouldn't attempt to hook into the Nuxt runtime as mentioned in the Module Migration Guide. 6. Compatibility Version: If you're using newer Node.js versions (19+), there might be compatibility issues in development mode as noted in some community reports. Consider trying Node.js 18 if possible. 7. Server Middleware vs. Nitro Plugins: Make sure you understand the difference between server middleware and Nitro plugins. Server middleware runs on every request, while Nitro plugins are for extending Nitro's runtime behavior Server Documentation. If none of these solutions work, you might need to check if there are any conflicts with other parts of your application or consider creating a minimal reproduction to isolate the issue. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@1338126650616840276> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Martin Malinda
Martin MalindaOP2mo ago
@kapa.ai difference between nuxtConfig.server nuxtConfig.devServer, nuxtConfig.nitro.devProxy and nuxtConfig.nitro.proxy and can any of this interfere with the files being autoregistered in server folder?
kapa.ai
kapa.ai2mo ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- I'll explain the differences between these Nuxt configuration options and whether they might interfere with server folder auto-registration. Configuration Options Comparison nuxtConfig.server This configures the Nuxt server itself, including options like port, host, and HTTPS settings. It's primarily for the development server configuration and doesn't directly affect the auto-registration of server files. nuxtConfig.devServer The devServerHandlers option (which I assume you're referring to) allows you to define Nitro development-only server handlers. As mentioned in the documentation:
devServerHandlers: Nitro development-only server handlers.
devServerHandlers: Nitro development-only server handlers.
Nuxt Configuration nuxtConfig.nitro.devProxy This is a development-only proxy configuration that allows you to forward requests to another server during development. It's useful for API proxying in development environments. nuxtConfig.nitro.proxy This is a more general proxy configuration for Nitro that works in both development and production. It's part of the Nitro configuration that you can set through the nitro key in your Nuxt config. Impact on Server Folder Auto-Registration None of these configuration options should directly interfere with the auto-registration of files in the server/ directory. The auto-registration of server middleware, API routes, and plugins is a core feature of Nuxt 3's server engine (Nitro). As mentioned in the documentation:
Nuxt automatically scans files inside these directories to register API and server handlers with Hot Module Replacement (HMR) support.
Server Directory Structure Potential Issues If you're experiencing issues with server files not being auto-registered, it could be related to: 1. Nitro Configuration: While the proxy settings shouldn't interfere, other Nitro configurations might affect server behavior. You can check your nitro configuration in nuxt.config.ts. 2. HMR Issues: There have been some reported issues with HMR for server files, especially with certain configurations or dependencies. For example, the issue you mentioned in your previous question might be related to a known issue with Tailwind v4 and server HMR as documented in a GitHub issue With Tailwind v4 HMR not working in server. 3. File Structure: Ensure your server files follow the correct structure and naming conventions. If you're still experiencing issues, you might want to try: - Restarting the Nuxt development server - Checking for any error messages in the console - Verifying that your server files export a default function defined with defineEventHandler() - Temporarily simplifying your Nitro configuration to isolate the issue Let me know if you need more specific help with your configuration! ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@1338126650616840276> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Martin Malinda
Martin MalindaOP2mo ago
@kapa.ai I'm trying to set
serverDir: '~~/server-test'
serverDir: '~~/server-test'
for an alternative server directory and if that folder does not exist, there's no error during server boot, I'd expect Nuxt would throw if this folder is not found
kapa.ai
kapa.ai2mo ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- I understand you're trying to configure an alternative server directory using serverDir: '~~/server-test' and you're wondering why Nuxt doesn't throw an error when this directory doesn't exist. According to the documentation, the serverDir option defines where Nuxt looks for Nitro routes, middleware, and plugins:
Define the server directory of your Nuxt application, where Nitro routes, middleware and plugins are kept. If a relative path is specified, it will be relative to your rootDir.
Nuxt Configuration The default value is "/<srcDir>/server", but you can customize it as you're trying to do. Nuxt doesn't throw an error when the specified serverDir doesn't exist because this is actually expected behavior. Nuxt doesn't require you to have a server directory at all - it's optional. If you don't have any server routes, middleware, or plugins, you don't need a server directory. This is similar to how Nuxt handles other directories like pages/ or components/ - if they don't exist, Nuxt simply won't load anything from them rather than throwing an error. If you want to verify that your serverDir configuration is being applied correctly, you could: 1. Create the server-test directory in your project root 2. Add a test middleware or API route in that directory 3. Check if it gets registered and works as expected This way, you can confirm that Nuxt is looking in the right place for server files. Would you like more information about how to structure your server directory or how to verify that your configuration is working correctly? ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@1338126650616840276> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?