javascript - how to share same nodejs port (3000) with multiple vhost(also nodejs app) -
relating vhost multiple domain apps on node server, i'm facing issue of not able call app.js of vhost doamin setup following:
root folder : website vhost folder: website/vietvan vhost folder: website/thanksme
the root app.js on root wbesite follow:
var vhost = require('vhost'); // module dependencies. var application_root = __dirname, express = require( 'express' ), vhost = require( 'vhost' ); function createvirtualhost(domainname, dirpath) { return vhost(domainname, express.static( dirpath )); } //create server var app = express(); //create virtual hosts var vietvanvhost = createvirtualhost("vietvan.net","./vietvan/"); var thanksmevhost = createvirtualhost("leean.me","./thanksme/"); //use virtual hosts app.use(thanksmevhost); app.use(vietvanvhost); //start server var port = 3000; app.listen( port, function() { console.log( 'express server listening on port %d in %s mode', port, app.settings.env ); });
on vietvan folder app.js follow:
let postroutes = require('./routes/post'); let userroutes = require('./routes/users'); let commentroutes = require('./routes/comment'); let approutes = require('./routes/app'); let app = express(); mongoose.promise = global.promise; mongoose.connect('mongodb://localhost/thanksme'); console.log('connected db'); // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'hbs'); // uncomment after placing favicon in /public app.use(favicon(path.join(__dirname, 'dist', 'favicon.ico'))); app.use(logger('dev')); app.use(bodyparser.json()); app.use(bodyparser.urlencoded({extended: false})); app.use(cookieparser()); app.use(express.static(path.join(__dirname, 'dist'))); app.use(function (req, res, next) { res.setheader('access-control-allow-origin', '*'); res.setheader('access-control-allow-headers', 'origin, x-requested-with, content-type, accept'); res.setheader('access-control-allow-methods', 'post, get, patch, delete, options'); next(); }); app.use('/user', userroutes); app.use('/post', postroutes); app.use('/comment', commentroutes); app.use('/', approutes); // catch 404 , forward error handler app.use(function (req, res, next) { return res.render('index'); }); // // app.listen(3000, function () { console.log('listening on port 3000 of vietvan.net'); // });
when start node pointing root app.js can not access vhost http://vietvan.net:3000, error: cannot /
but if start node point /websites/vietvan/app.js or change vietvanvhost = createvirtualhost("vietvan.net","./vietvan/"); "/vietvan/dist' in createvirtualhost() ok , start serving compiled code in dist folder of vietvan folder
seem app.js in vietvan folder did dont work, or did not called root app.js cause of issue
can point out got mistake? , how fix issue ?
thank much
Comments
Post a Comment