我正在使用Express在node.js上构建应用程序。我的应用程序在端口3001上运行。我想为我的应用程序设置虚拟主机,这样当我使用http://local.com:3001时,它应该引导我到我的应用程序的主页。
我的节点版本是:v0.12.3
快递版:4.12.4
NPM版本:2.9.1
我在/ etc / hosts中添加了主机。 我的申请是在/ var / www / local。我在/ var / www / vhost下设置了一个快速应用程序作为vhost管理器。经过大量研究,我尝试使用以下两种方法,但没有一种方法有效:
1)/ var / www / vhost的app.js
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var vhost = require('vhost');
var app = express();
var main = express();
if (!module.parent) main.use(logger('dev'));
main.get('/', function(req, res){
res.send('Hello from main app!');
});
main.get('/:sub', function(req, res){
res.send('requested ' + req.params.sub);
});
// Redirect app
var redirect = express();
redirect.use(function(req, res){
if (!module.parent) console.log(req.vhost);
res.redirect('http://local.com:3001/' + req.vhost[0]);
});
// Vhost app
var app = module.exports = express();
app.use(vhost('*.local.com', redirect)); // Serves all subdomains via Redirect app
app.use(vhost('local.com', main)); // Serves top level domain via Main server app
if (!module.parent) {
app.listen(3001);
console.log('Express started on port 3001');
}
2)使用express-vhost
的/ var / www / vhost app.jsvar evh = require('express-vhost'),
express = require('express');
var appFactory = function(echo) {
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var routes = require('./routes/index');
var users = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/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, 'public')));
app.use('/', routes);
app.use('/users', users);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;
return app;
};
var server = express();
server.use(evh.vhost(server.enabled('trust proxy')));
server.listen(3006);
evh.register('http://local.com:3001', appFactory('/var/www/local/app.js'));
如果我在这里做错了任何建议,或者有任何其他方法来实现node.js上的express虚拟主机的概念将非常有帮助。在此先感谢:)