63

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?

peterh
  • 11,875
  • 18
  • 85
  • 108
gremo
  • 47,186
  • 75
  • 257
  • 421

3 Answers3

81

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);
});
Tal Avissar
  • 10,088
  • 6
  • 45
  • 70
Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
  • Well thanks. I already have concat/min/lint tasks, but when i try to run the server (`grunt server` command) configured as `http://localhost:8080` with files and folders, it soon quits with "task done" message. – gremo Aug 13 '12 at 23:51
  • 2
    @Gremo That's actually [by design](https://github.com/cowboy/grunt/blob/master/docs/task_server.md#about-%E2%9A%91): "*Once grunt's tasks have completed, the web server stops.*" It's not intended for starting the server for indefinite/production use; just for having it available to other tasks, especially tests that could imitate a web-client ([`http.request()`](http://nodejs.org/api/http.html#http_http_request_options_callback) or [`request`](https://npmjs.org/package/request)). – Jonathan Lonowski Aug 14 '12 at 00:15
  • That is i can't use the server just for running my application, right? – gremo Aug 14 '12 at 00:21
  • @Gremo Not normally. You could define the custom task as asynchronous, which will keep grunt alive while the server is still running. See my edit. – Jonathan Lonowski Aug 14 '12 at 01:18
  • 14
    The server runs for as long as grunt is busy with tasks. A quick and dirty way to keep it opened is with grunt.registerTask("run", "server watch");. To cancel the watch will close the browser. – widged Aug 28 '12 at 06:12
  • 4
    props, @widged! `grunt.registerTask("run", "server watch")` is very clever. Why not make this a separate answer so it's more visible? – Chris Calo Nov 25 '12 at 01:38
  • And now, how do I use node-inspector? I have the "Error: connect ECONNREFUSED Is node running with --debug port 5858?" error – Totty.js Oct 18 '13 at 14:25
  • Live reload doesn't work anymore, and are loaded some cached files.. Don't know why, but is not the last version I have in my app folder. – Totty.js Oct 18 '13 at 14:35
  • @Totty Sorry. But, comments aren't the best place further Q&A. Have you posted those as questions? – Jonathan Lonowski Oct 18 '13 at 14:36
  • Not yet, but I will soon. – Totty.js Oct 18 '13 at 14:48
55

The server task is now the connect task and it's included in the grunt-contrib-connect package.

The connect task starts a connect web server.

Install this plugin with this command:

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

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

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

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

Run this task with the grunt connect command.

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 keepalive option, and can be enabled ad-hoc by running the task like grunt connect:targetname:keepalive. targetname is equal to "server" in the code sample below.

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-root directory relative to the Gruntfile, and any tasks run afterwards will be able to access it.

// Project configuration.
grunt.initConfig({
  connect: {
    server: {
      options: {
        port: 9001,
        base: 'www-root'
      }
    }
  }
});
ComFreek
  • 29,044
  • 18
  • 104
  • 156
Giovanni Cappellotto
  • 4,597
  • 1
  • 30
  • 33
8

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.

David Souther
  • 8,125
  • 2
  • 36
  • 53