2014-05-05 18 views
3

我通常咕嘟咕嘟开始我的服务器的NodeJS,类似于一个任务:使用虚拟主机时,可以运行Gulp或Grunt任务吗?

gulp.task('server', function(){ 

    var port = gulp.env.port || 80; 

    plugins.nodemon({ 
    script: 'server.js', 
    ignore: '*', 
    }).on('start', ['source','watch:source']); 

}); 

,编译所有的翡翠&萨斯模板,并转储到一个文件夹中的资产。

对于我目前的项目,我介绍了vhosts,这样我就可以为不同的节点应用程序提供[X] .mysite.com。我不知道如何在启动父服务器时运行我的gulp文件。我是否应该在父目录中放入一个gulp文件,并将每个vhost应用程序的所有任务都包含在该目录中? 'npm install'和'bower install'同样的问题。

tl; dr:如何在使用ExpressJS时为每个虚拟主机应用程序运行Gulp或Grunt任务? 'npm install'和'bower install'同样的问题。

+0

vhost表示您正在使用服务器(如nginx)来配置路由。我不知道你作为虚拟主机提出的问题(假设nginx或apache)只是你的节点应用的代理 – SteveLacy

+1

不,我正在使用vhost中间件进行Express。没有nginx。 – elliottregan

回答

1

使用这种文件夹结构:

vhost(d) 
    - website1(d) 
    - app.js 
    - public(d) 
     - index.html 
    - website2(d) 
    - app.js 
    - public 
     - index.html 
server.js 

您server.js文件应该是这样:

"use strict"; 
    var 
    express = require('express'), 
    vhost = require('vhost'), 
    app = express(), 
    website1 = require('./vhost/website1/app'), 
    website2 = require('./vhost/website2/app'), 
    port = process.env.PORT || 8080; 

app   
    .use(vhost('localhost', website)) 
    .use(vhost('cms.localhost', cms)) 
    .listen(port, function() { 
    console.log('server listening on port ' + port); 
    }); 

您app.js在WEBSITE1文件夹中的文件应该是这样的:

var 
express = require('express'), 
app = express() 

app.use(express.static(__dirname + '/public')); 
app.get('/', function(req, resp) { 
    resp.sendFile(__dirname + '/public/index.html'); 
}); 

module.exports = app; 

同样的事情应该是为网站2/app.js文件

写信你一饮而尽任务,并在项目运行的主目录启动server.js

gulp.task('serve', function(){ 

    var port = gulp.env.port || 8080; 

    plugins.nodemon({ 
    script: 'server.js', 
    ignore: '*', 
    }).on('start', ['source','watch:source']); 

}); 

gulp serve 

享受!

相关问题