关于节点服务器上多个域应用程序的vhost,我现在面临一个无法调用vhost域的app.js的问题。
我的设置如下:
root folder : website
vhost folder: website/vietvan
vhost folder: website/thanksme
根网站上的根app.js如下:
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 the virtual hosts
var vietvanVhost = createVirtualHost("vietvan.net","./vietvan/");
var thanksmeVhost = createVirtualHost("leean.me","./thanksme/");
//Use the 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 );
});
在vietvan文件夹中,app.js如下:
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 to DB');
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
// uncomment after placing your 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 and forward to 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');
// });
启动指向根app.js的节点时
我无法访问vhost http://vietvan.net:3000
,错误:
无法获取/
但是,如果我将节点指向/websites/vietvan/app.js
或将vietvanVhost = createVirtualHost("vietvan.net","./vietvan/");
更改为/vietvan/dist
中的createVirtualHost()
,那么就可以了,并开始在dist文件夹中提供已编译的代码vietvan文件夹。
似乎vietvan文件夹中的app.js无法正常工作,或者没有被root app.js调用,从而导致问题。
任何人都可以帮忙指出我犯了哪个错误吗?以及如何解决这个问题?
非常感谢。