Wednesday, 13 February 2019

Nuxt module that adds another website as serverMiddleware

The idea

First you have a main Nuxt website like any other. Then you add my module to your project. My module then adds a subdomain "admin.example.com" to your project which is a fully fleshed out Nuxt based website all of its own but running from your projects process, so instead of making two website projects that have to be started separately the idea is that I can make my one website project and then one module project which adds the other website, turning three projects into two.

Code

module.js

this.addServerMiddleware(vhost('admin.website.com', adminApp));

adminApp.js

const {Nuxt, Builder} = require('nuxt');

const config = require('../admin/nuxt.config');
config.dev = true;

const nuxt = new Nuxt(config);

if (nuxt.options.dev) {
  new Builder(nuxt).build()
    .catch(function(error) {
      console.log(error);
    })
}

module.exports = nuxt.render;

Part of nuxt.config.js from the admin website

// __dirname leads to my modules directory
// process.cwd() leads to the main websites folder
module.exports = {
  mode: 'universal',
  srcDir: __dirname,
  rootDir: __dirname,
  modulesDir: process.cwd(),

The problem

Whenever I go to "admin.website.com" it results in the following message:

enter image description here

I've tried tonnes various changes and tweaks already but nothing seems to be working. As far as I can tell it might have to do with the fact that either A) the website gets built somewhere it doesn't find the generated resource files, or B) that I haven't prayed enough to the spaghetti monster.

I've tried exporting adminApp as an Express app that uses app.use(nuxt.render) but that results in the same error so I'm certain that's not the issue. I've tried using an Express app for api.website.com and that works just fine, so I know I'm like THIS |--| close to making this all work. Adding subdomains like this does work and each subdomain can be an Express app, but for some reason Nuxt can't find any resources when used this way. According to https://nuxtjs.org/api/nuxt-render/ nuxt.render(req, res) as you can see takes req and res as arguments, which should mean I don't need to use it via an Express app, serverMiddleware is already an Express app.

Thoughts?



from Nuxt module that adds another website as serverMiddleware

No comments:

Post a Comment