What is the purpose of the gruntjs server task?

I am learning how to promote the use of gruntjs. I found a server task , but I can not understand.

Can I use mapped / mined server task mapping files to test my application (uses backbone.js) without moving or placing the source files in the root web server? For example, without apache.

If not, then what is the intended use of the server task?

+61
gruntjs
Aug 13 '12 at 22:28
source share
3 answers

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

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

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

It will function similarly to the Apache server, serving static files based on their path, but using the http module via connect to configure it ( source ).

If you need it to serve more than just static files, you will need to consider the definition of a server user 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 the user server instance:

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



Can I use mapped / mined server task mapping files to test my application [...]

Concatenation and minimization have their own tasks - concat and min - but can be used together with the server task to complete all 3.




Edit

If you want it to keep the server (as well as grunt) for some time, you could define the task as asynchronous (from the server '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); }); 
+80
Aug 13 2018-12-12T00:
source share

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

The connect task starts the connection web server.

Install this plugin with this command:

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

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

Once the plugin has been installed, it can be included inside your Grunt file with this JavaScript line:

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

Run this task with the grunt connect command.

Please note that this server only works while grunt is running. After the grunt jobs are completed, the web server stops. This behavior can be changed using the keepalive parameter, and it can be enabled ad-hoc by completing a task, for example grunt connect:targetname:keepalive . targetname in the sample code is "server".

In this example, grunt connect (or in more detail, grunt connect:server ) will launch a static web server at http://localhost:9001/ , and its base path will be set to the www-root directory relative to the Grunt file, and any tasks that are run after this will be able to access it.

 // Project configuration. grunt.initConfig({ connect: { server: { options: { port: 9001, base: 'www-root' } } } }); 
+53
Aug 08 '13 at 7:23
source share

The server’s task point is 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 life cycle to get static testing components into the test environment. Use a fully-fledged server, possibly managed by NPM life-cycle scenarios, for production environments.

+8
Dec 27
source share



All Articles