Use Nuxt with Express.js instead of nitro
Hello everyone, I really need some help I dont know how many hours I have spent on this simple task and yet I can not fix it.
I want to use nuxt with express.js for the back instead of nitro but I want of course to keep nuxt's auto routing for the front end.
I have made a project , added this to nuxt.config.ts
export default defineNuxtConfig({
ssr: false,
compatibilityDate: '2025-05-15',
devtools: { enabled: true },
serverHandlers: [
{
route: '/api/express',
handler: '~~/middleware-express-js/app.ts',
middleware: true
}
]
})
But I am getting this error
" [unhandledRejection] read ECONNRESET
at TCP.onStreamRead (node:internal/stream_base_commons:216:20)" Is this even the correct way to use express with nitro? This is an example fetch:
at TCP.onStreamRead (node:internal/stream_base_commons:216:20)" Is this even the correct way to use express with nitro? This is an example fetch:
<template>
<div>
This is the template index.vue file.
</div>
<div>
{{ expressData }}
</div>
</template>
<script setup lang="ts">
const { data: expressData } = await useFetch("/api/express");
</script>
And this is an example post I am using:
<template>
<div>
Template add-product.vue page.
</div>
<form action="/api/express/product" method="POST">
<div>
<input type="text" name="title">
</div>
<button type="submit">Add Product</button>
</form>
</template>
<script setup>
</script>
4 Replies
Hi @Tekimoto. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
Using Express.js directly as a backend within a Nuxt 3 project (which uses Nitro as its server engine) is not officially supported in the way you might expect from Nuxt 2's serverMiddleware. In Nuxt 3, Nitro is tightly integrated and handles all server-side logic, including API routes and middleware. While you can add custom handlers, Nitro expects them to be standard event handlers (using defineEventHandler), not full Express apps.
Key Points from the Knowledge Sources:
- Nuxt 3's server engine is Nitro, which is designed to handle API routes and server logic using its own conventions and the h3 framework, not Express.js. Files in server/api, server/routes, and server/middleware are automatically registered as handlers by Nitro, and these should use defineEventHandler, not Express middleware or routers Docs: server directory.
- The serverHandlers option in nuxt.config is for registering Nitro-compatible handlers, not Express apps. The handler should be a file exporting a function compatible with Nitro/h3, not an Express app Docs: serverHandlers.
- Attempting to use Express.js directly as a handler (as in your example) will likely result in errors or unexpected behavior, such as the ECONNRESET error you are seeing. This is because Nitro and Express have different expectations for request/response handling and lifecycle.
- There is a YouTube video that explores running other backend frameworks (like Express) "inside" Nitro, but the conclusion is that you can't avoid Nitro—it's always the underlying server, and integrating Express is not straightforward or recommended. The video demonstrates that while you can technically mount Express inside a Nitro handler, it's not a supported or robust solution, and you lose many Nitro features YouTube: Nuxt with OTHER backend frameworks?.
Recommended Approach:
- If you want to use Nuxt 3's frontend features (auto routing, etc.), use Nitro for your backend logic (API routes, middleware) and write your handlers using defineEventHandler.
- If you must use Express.js, run it as a completely separate server (on a different port), and proxy API requests from Nuxt to Express, either via a reverse proxy (nginx, etc.) or by using fetch/axios to communicate between the two servers. This is shown in the Stack Overflow example, where Express runs separately and Nuxt proxies API requests to it Stack Overflow: How to use Node.js with Nuxt.js?.
Example: Nitro API Handler (Recommended)
If you must use Express:
- Run Express on a separate port (e.g., 3001).
- Proxy /api/* requests to Express using nginx or similar, as shown in the Stack Overflow answer Stack Overflow: How to use Node.js with Nuxt.js?.
Summary:
You cannot directly use Express.js as a backend within Nuxt 3/Nitro by registering it as a handler. Nitro expects its own handler format. For full compatibility and best developer experience, use Nitro's API routes. If you need Express, run it separately and proxy requests. I'm sorry, but there's no officially supported way to replace Nitro with Express.js as the backend engine in Nuxt 3. Nitro is a core part of Nuxt 3's architecture and cannot be swapped out for Express in the way you could with Nuxt 2's serverMiddleware. For more details, see the Nuxt server directory docs and YouTube: Nuxt with OTHER backend frameworks?. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
You cannot directly use Express.js as a backend within Nuxt 3/Nitro by registering it as a handler. Nitro expects its own handler format. For full compatibility and best developer experience, use Nitro's API routes. If you need Express, run it separately and proxy requests. I'm sorry, but there's no officially supported way to replace Nitro with Express.js as the backend engine in Nuxt 3. Nitro is a core part of Nuxt 3's architecture and cannot be swapped out for Express in the way you could with Nuxt 2's serverMiddleware. For more details, see the Nuxt server directory docs and YouTube: Nuxt with OTHER backend frameworks?. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
<@213738053108236288> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
hello, i can help u.
Did you try
https://youtu.be/x81InsU6t0E
Alexander Lichter
YouTube
Nuxt with OTHER backend frameworks?
Nuxt and Nitro are dream team. But throughout the time, many people wondered if you could use Nuxt also with another backend framework - without using a separate instance of course (as that works anyway). So, why not giving it a try? In this video, we try to get three different backend frameworks up and running, from classic (but old) express, f...