N
Nuxt4mo ago
Ali

How to Serve a Nuxt 3 App with Express: Replacing `nuxt.render` and Middleware Setup?

Hey everyone, I'm trying to serve a Nuxt 3 application using an Express server. My project structure is as follows:
/root
/client # Nuxt 3 application
/app # Express server for APIs
/node_modules
.env
package.json
/root
/client # Nuxt 3 application
/app # Express server for APIs
/node_modules
.env
package.json
I want the Express server to serve the built Nuxt 3 app. In Nuxt 2, I used nuxt.render, but I'm struggling to find the equivalent in Nuxt 3. Here’s my current setup for the Express server (server.js):
'use strict';

const express = require('express');
const http = require('http');
const path = require('path');
const { loadNuxt, build } = require('@nuxt/kit');

const app = express();
const server = http.Server(app);

(async function () {
const nuxt = await loadNuxt({ for: 'start', config: { rootDir: path.resolve(__dirname, '../client') } });

// Apply Nuxt as middleware
app.use(nuxt);

// Serve static files from Nuxt build directory
app.use(express.static(path.resolve(__dirname, '../client/.output/public')));

// Start the server
server.listen(3000, () => {
console.log('Server is listening on http://localhost:3000');
});
})();
'use strict';

const express = require('express');
const http = require('http');
const path = require('path');
const { loadNuxt, build } = require('@nuxt/kit');

const app = express();
const server = http.Server(app);

(async function () {
const nuxt = await loadNuxt({ for: 'start', config: { rootDir: path.resolve(__dirname, '../client') } });

// Apply Nuxt as middleware
app.use(nuxt);

// Serve static files from Nuxt build directory
app.use(express.static(path.resolve(__dirname, '../client/.output/public')));

// Start the server
server.listen(3000, () => {
console.log('Server is listening on http://localhost:3000');
});
})();
However, I'm encountering the following issues: app.use() requires a middleware function: I initially tried to pass nuxt.render, but it seems there's no direct equivalent in Nuxt 3. Serving the static files: I'm not sure if I'm pointing to the correct directory to serve the build files from Nuxt 3. Has anyone successfully integrated Nuxt 3 with Express? What’s the proper way to replace nuxt.render in Nuxt 3, and how should I set up the middleware and static file serving correctly? Any guidance would be greatly appreciated!
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server