gruntjs服务器任务的目的是什么?

编程入门 行业动态 更新时间:2024-10-09 14:25:07
本文介绍了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 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服务器任务的目的是什么?

本文发布于:2023-11-09 23:34:08,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:目的   服务器   gruntjs

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!