Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the purpose of gruntjs server task?

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?

like image 922
gremo Avatar asked Aug 13 '12 22:08

gremo


People also ask

What is grunt used for?

Grunt is a JavaScript task runner, a tool used to automatically perform frequent tasks such as minification, compilation, unit testing, and linting. It uses a command-line interface to run custom tasks defined in a file (known as a Gruntfile). Grunt was created by Ben Alman and is written in Node. js.

What is a grunt server?

Grunt is a command line Javascript task runner utilizing Node. js platform. It runs custom defined repetitious tasks and manages process automation.

What is gulp and grunt used for?

Using a bit of fairly simple code, plugins and either Gulp or Grunt, you can set up a process that automates tasks. Both of these tools allow you to check for new files or changes to files in certain directories and to run tasks applicable to them.

Is grunt still used?

The Grunt community is still going strong and both tools look like they're going to be around for a while yet. I should mention that another up and coming alternative to task runners like Grunt and Gulp is simply using npm scripts with command-line tools.


2 Answers

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); }); 
like image 58
Jonathan Lonowski Avatar answered Oct 24 '22 10:10

Jonathan Lonowski


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'       }     }   } }); 
like image 33
Giovanni Cappellotto Avatar answered Oct 24 '22 12:10

Giovanni Cappellotto