gruntjs服务器任务的目的是什么? [英] What's the purpose of gruntjs server task?

查看:79
本文介绍了gruntjs服务器任务的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何推动使用gruntjs。我找到了服务器任务,但我无法获得我可以使用服务器任务映射级联/缩小文件来测试我的应用程序(使用backbone.js),而无需移动或将源文件放置在Web服务器根目录中? 如果没有apache的话。



如果不是,那么服务器任务的用处是什么? / div>

服务器任务用于启动一个静态服务器,其中 base 路径设置为web例如:将 ./ web-root 作为 http:// localhost服务。 :8080 /

  grunt.initConfig({
server:{
端口:8080,
base:'./web-root'
}
});

它的功能类似于Apache服务器,根据路径提供静态文件,但使用 http模块通过连接设置它()。



如果您需要它不仅仅提供静态文件,那么您需要考虑定义一个自定义的服务器 a>:

  grunt.registerTask('server','启动一个自定义的web服务器',function(){
grunt.log.writeln('在端口1234上启动web服务器');
require('./ server.js')。listen(1234);
});

以及自定义服务器实例:

  // server.js 
var http = require('http');
module.exports = http.createServer(function(req,res){
// ...
});







我可以使用服务器任务映射连接/缩小文件来测试我的应用程序[/ b]

连接和缩小有自己的专用任务 - concat min - 但可以与服务器任务一起使用来完成所有3个操作。






编辑



如果你想让服务器保持一段时间(以及grunt),你可以将任务定义为异步 (使用服务器的 'close' event ):

  grunt.registerTask('server', '启动一个自定义的web服务器',function(){
var done = this.async();
grunt.log.writeln('在端口1234上启动web服务器');
require('./ server.js')。listen(1234).on('close',done);
});


I'm learning how to propel use gruntjs. I found the server task but I can't get the point.

Can i use the server task mapping concatenated/minified files to test my application (uses backbone.js) without moving or placing source files in web server root? Without apache for example.

If no, what's the supposed use of server task?

解决方案

The server task is used to start a static server with the base path set as the web root.

Example: Serve ./web-root as http://localhost:8080/:

grunt.initConfig({
  server: {
    port: 8080,
    base: './web-root'
  }
});

It will function similar to an Apache server, serving up static files based on their path, but uses the http module via connect to set it up (source).

If you need it to serve more than just static files, then you'll want to consider defining a custom server task:

grunt.registerTask('server', 'Start a custom web server.', function() {
  grunt.log.writeln('Starting web server on port 1234.');
  require('./server.js').listen(1234);
});

And custom server instance:

// server.js
var http = require('http');
module.exports = http.createServer(function (req, res) {
    // ...
});


Can I use the server task mapping concatenated/minified files to test my application [...]

Concatenation and minification have their own dedicated tasks -- concat and min -- but could be used along with a server task to accomplish all 3.


Edit

If you want it to persist the server for a while (as well as grunt), you could define the task as asynchronous (with the server's 'close' event):

grunt.registerTask('server', 'Start a custom web server.', function() {
  var done = this.async();
  grunt.log.writeln('Starting web server on port 1234.');
  require('./server.js').listen(1234).on('close', done);
});

这篇关于gruntjs服务器任务的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆