Express Vhost 'argument handle is required'

922 Views Asked by At

Firstly, found a couple of similar question on here but no duplicates, I think my situation is slightly different.

Trying to get a website and associated API working on Express using vhost for subdomians.

Here is my folder structure

/api
  api.js
/server
  website.js
server.js

My server.js

const vhost = require('vhost');
const express = require('express');

const app = express();
app.use(vhost('localhost', require('./server/website.js').app));
app.use(vhost('api.localhost', require('./api/api.js').app));

app.listen(1337, () => {});

My api.js

const express = require('express');
const app = express();

app.get('/', function(req, res){
    res.send({ hello: 'world' });
});

module.exports = app;

Initially my path to api.js was wrong and I got a not found error so now I know my path is right but now I get the error "Typeerror: argument handle is required" whatever I do.

Any help would really be appreciated.

2

There are 2 best solutions below

0
On

Your exporting the app already. So it is not necessary to add .app to the end of your require.

It should be:

app.use(vhost('localhost', require('./server/website')));
app.use(vhost('api.localhost', require('./api/api')));

Hope that helps.

0
On

here's what I did:

//used the api.localhost as the subdomain url
//it requires another express app.js to work
//the other express app must -> module.exports = app;

const vhost = require('vhost');

const app = express();
app.use(vhost('api.localhost', require('./api/app')));