node.js gruntjs 服务器任务的目的是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11943212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:09:37  来源:igfitidea点击:

What's the purpose of gruntjs server task?

node.jsgruntjs

提问by gremo

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

我正在学习如何推动使用 gruntjs。我找到了服务器任务,但我不明白这一点。

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.

我可以使用服务器任务映射连接/缩小文件来测试我的应用程序(使用backbone.js)而不移动或将源文件放置在 Web 服务器根目录中吗?例如,没有 apache。

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

如果不是,服务器任务的假设用途是什么?

回答by Jonathan Lonowski

The servertask is used to start a static server with the basepath set as the web root.

server任务用于启动一个静态服务器,base路径设置为 web 根。

Example: Serve ./web-rootas http://localhost:8080/:

示例:./web-root充当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 modulevia connectto set it up (source).

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

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

如果您需要它提供的不仅仅是静态文件,那么您需要考虑定义一个自定义server任务

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 -- concatand min-- but could be used along with a servertask to accomplish all 3.

串联和缩小有自己的专用任务——concat并且min——但可以与server完成所有 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),您可以将任务定义为异步(使用服务器的'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);
});

回答by Giovanni Cappellotto

The servertask is now the connecttask and it's included in the grunt-contrib-connectpackage.

server现在的任务是connect任务,它包含在grunt-contrib-connect包中。

The connecttask starts a connect web server.

connect任务启动连接 Web 服务器。

Install this plugin with this command:

使用以下命令安装此插件:

npm install grunt-contrib-connect --save-dev

Note: --save-devincludes the package in your devDependencies, see https://npmjs.org/doc/install.html

注意:--save-dev在您的 中包含该包devDependencies,请参阅https://npmjs.org/doc/install.html

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

安装插件后,可以使用以下 JavaScript 代码在 Gruntfile 中启用它:

grunt.loadNpmTasks('grunt-contrib-connect');

Run this task with the grunt connectcommand.

使用grunt connect命令运行此任务。

Note that this server only runs as long as grunt is running. Once grunt's tasks have completed, the web server stops. This behavior can be changed with the keepaliveoption, and can be enabled ad-hoc by running the task like grunt connect:targetname:keepalive. targetnameis equal to "server" in the code sample below.

请注意,此服务器仅在 grunt 运行时才运行。一旦 grunt 的任务完成,Web 服务器就会停止。此行为可以通过keepalive选项更改,并且可以通过运行像grunt connect:targetname:keepalive. targetname在下面的代码示例中等于“服务器”。

In this example, grunt connect(or more verbosely, grunt connect:server) will start a static web server at http://localhost:9001/, with its base path set to the www-rootdirectory relative to the Gruntfile, and any tasks run afterwards will be able to access it.

在这个例子中,grunt connect(或更详细地说,grunt connect:server)将在 启动一个静态 Web 服务器http://localhost:9001/,其基本路径设置为www-root相对于 Gruntfile的目录,之后运行的任何任务都可以访问它。

// Project configuration.
grunt.initConfig({
  connect: {
    server: {
      options: {
        port: 9001,
        base: 'www-root'
      }
    }
  }
});

回答by David Souther

The point of the server task is to have quick and dirty access to static files for testing. grunt server IS NOT a production server environment. It really should only be used during the grunt lifecycle to get static testing assets to the testing environment. Use a full-fledged server, possibly controlled by the NPM lifecycle scripts, for production environments.

服务器任务的重点是快速访问静态文件以进行测试。grunt 服务器不是生产服务器环境。它真的应该只在 grunt 生命周期中使用,以将静态测试资产引入测试环境。将可能由 NPM 生命周期脚本控制的成熟服务器用于生产环境。