I am trying to run 2 or more nuxt apps using expressjs and its vhost feature. My aim is to run multiple nuxt apps assigned to different domains in one single port.
Here is what I tried.
- I set up 2 nuxt sample apps
- I set up expressjs with vhost module added to the package.json
Here is the content of server.js for expressjs
var vhost = require("express"); var vhost = require("vhost"); var app = (module.exports = express()); var app_one = require("./app_one/server/index.js"); var app_two = require("./app_two/server/index.js"); app .use(vhost("appone.com", app_one.start_app_one)) .use(vhost("apptwo.com", app_two.start_app_two)) .listen(3000);
Then I added index.js under server directory in both of the applications.
App One
const express = require("express");
const consola = require("consola");
const {
Nuxt,
Builder
} = require("nuxt");
const app = express();
// Import and Set Nuxt.js options
const config = require("../nuxt.config.js");
config.dev = process.env.NODE_ENV !== "production";
async function start() {
// Init Nuxt.js
const nuxt = new Nuxt(config);
const {
host,
port
} = nuxt.options.server;
// Build only in dev mode
await nuxt.ready();
// Give nuxt middleware to express
app.use(nuxt.render);
// Listen the server
app.listen(port, host);
consola.ready({
message: `Server listening on http://${host}:${port}`,
badge: true
});
}
exports.start_app_one = function () {
start();
};
App Two
const express = require("express");
const consola = require("consola");
const {
Nuxt,
Builder
} = require("nuxt");
const app = express();
// Import and Set Nuxt.js options
const config = require("../nuxt.config.js");
config.dev = process.env.NODE_ENV !== "production";
async function start() {
// Init Nuxt.js
const nuxt = new Nuxt(config);
const {
host,
port
} = nuxt.options.server;
await nuxt.ready();
// Give nuxt middleware to express
app.use(nuxt.render);
// Listen the server
app.listen(port, host);
consola.ready({
message: `Server listening on http://${host}:${port}`,
badge: true
});
}
exports.start_app_two = function () {
start();
};
Then I run node server.js
. When I try to access appone.com:3000
it just shows nuxt loading webpage and stops there. No errors or no messages inside console. I am stuck at this point, Can someone help me?
So I made it work. Here is how I did it.
Here is the content of server.js for expressjs
Created 2 nuxt with expressjs server using the command below
npx create-nuxt-app <project-name>
Edited the server/index.js files from both apps remove the app.listen part as we are already listening in the needed port and host using the expressjs server.js file.
Here is the index.js file from both apps.
Now I went to each app-specific folders and I run the below command to build the files
npm run build
I included the modules needed by my child apps because it was not working without those for some reason.
For the first app
For the second app.
node server.js
and both apps were accessible from their respective domains ieappone.com:3000
andapptwo.com:3000
without any issues