在服务器上发布项目

时间:2014-11-09 09:39:02

标签: javascript node.js gruntjs yeoman

我使用Yeoman' generator-webapp'创建了一个项目。这包括名为' connect'的Grunt任务,用于在服务器上运行项目。目前它在我的localhost上运行。任何人都可以向我解释如何配置它以在不同的服务器上运行吗?

我有一个我可以使用的大学提供的服务器。我们假设用户名为myUsername和密码myPassword的xyz.abc.com。

Grunt任务的定义如下:

// The actual grunt server settings
connect: {
  options: {
    port: 9000,
    open: true,
    livereload: 35729,
    // Change this to '0.0.0.0' to access the server from outside
    hostname: '0.0.0.0'
  },
  livereload: {
    options: {
      middleware: function(connect) {
        return [
          connect.static('.tmp'),
          connect().use('/bower_components', connect.static('./bower_components')),
          connect.static(config.app)
        ];
      }
    }
  },
  test: {
    options: {
      open: false,
      port: 9001,
      middleware: function(connect) {
        return [
          connect.static('.tmp'),
          connect.static('test'),
          connect().use('/bower_components', connect.static('./bower_components')),
          connect.static(config.app)
        ];
      }
    }
  },
  dist: {
    options: {
      base: '<%= config.dist %>',
      livereload: false
    }
  }
},

1 个答案:

答案 0 :(得分:1)

如果你想在远程服务器上使用grunt部署你的应用程序,你可以像我一样使用类似的approch:在你的yeoman deploy grunt任务中添加一个新的build参数或者创建一个新的deploy专用任务,使用:

  • grunt-ssh用于通过SSH执行linux命令并通过SFTP发送文件
  • grunt-zipup用于在通过SFTP发送本地资源之前将其删除

例如,我有一个编译/优化/缩小/修订资源的构建任务,其中我添加了deploy参数:

grunt.registerTask(
    'build',
    'Build task, does everything',
    function() {

        var tasks = [
            [...], // custom build tasks
            'zipup:buildClient' // end of build generates a zip package
        ];

        if (grunt.option('deploy')) {
            tasks.push('sshexec:cleanApacheDir'); // empty remote folder for a fresh new install
            tasks.push('sftp:sendZipToApache');   // send zip through SFTP
            tasks.push('sshexec:unzipToApache');  // unzip trough SSH command `unzip`
        }

        grunt.task.run(tasks);
    }
);

有关详细信息,请参阅模块文档

ps:你可以使用大量的其他grunt插件。