module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
server: {
port: 8888,
base: \'.\'
}
});
};
C:\\P
By default Grunt starts up the server just for testing (or any other task asked..) and as soon as it's done it exits....
But fortunately I found a solution which by adding this to your grunt.js
file will let you (optionally) halt the server from exiting.
grunt.registerTask('wait', 'Wait for a set amount of time.', function(delay) {
var d = delay ? delay + ' second' + (delay === '1' ? '' : 's') : 'forever';
grunt.log.write('Waiting ' + d + '...');
// Make this task asynchronous. Grunt will not continue processing
// subsequent tasks until done() is called.
var done = this.async();
// If a delay was specified, call done() after that many seconds.
if (delay) { setTimeout(done, delay * 1000); }
});
Then in your command line call it: grunt server wait
then you should be able to see it in the browser..
Make sure you add it inside module.exports = function(grunt){...}
Don't use grunt to serve your project. Grunt is a build tool. Instead, use npm lifecycle scripts.
server.js
var express = require("express"),
app = express();
app.use('/', express.static(__dirname));
app.listen(8888);
package.json
{
"name": "my-project",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"express": "3"
}
}
Now you can run npm start
and life will be great. Grunt is a build tool, not a server. npm is a package lifecycle manager, not a build tool. Express is a server library. Use each in its right place.
The exception to this rule is when you're needing to serve your project to other testing tools in your build stack. The grunt-contrib-connect
plugin is designed specifically with this use case in mind, and has a keepalive
configuration setting that will leave grunt open while serving your static files. This is usually used in conjunction with a watch
task that runs a test suite when either the tests or the code changes.
The server
task only runs as long as it is needed, but you can keep it from quitting. From a comment by widget on another question: In your grunt.js
file define a task named run
that runs the tasks server
and watch
.
grunt.registerTask("run", "server watch");
The watch
task runs indefinitely, so it prevents the server
task from ending. Just make sure you also have a config for the watch
task. Here it is all together in your grunt.js
file:
module.exports = function (grunt) {
// …
grunt.initConfig({
// …
watch: {
files: "<config:lint.files>",
tasks: "lint qunit",
},
// …
});
grunt.registerTask("run", "server watch");
};
From the command line just type:
$ grunt run
The server will stay up and running.
Alternatively, as @NateBarr points out, from the command line you can run:
$ grunt server watch
In grunt 0.4 combined with grunt-contrib-connect you can run a long running server by using the keepalive
argument: grunt connect:target:keepalive
or define it as an option in your config:
grunt.initConfig({
connect: {
target:{
options: {
port: 9001,
keepalive: true
}
}
}
});