Last Updated: February 25, 2016
·
24.98K
· usharf

Using a custom grunt task to start a node server, and watch

Grunt's server task starts a static server (very useful when developing single page apps), and is configured like this:

grunt.initConfig({
    server: {
       port: 3000,
       base: './public'
    }
});

I can keep it running by using it with watch (must be the last, since it blocks).

grunt server watch

To have it start a Node app, instead of just serving static assets, I use a custom task:

grunt.registerTask('server', 'Start a custom web server', function() {
    grunt.log.writeln('Started web server on port 3000');
    require('./app.js').listen(3000);
});

I then export it in app.js like this:

express = require('express');
app = module.exports = express();
....

2 Responses
Add your response

Thanks! Simple but good tip. :)

Btw check out <a href="https://npmjs.org/package/tiny-lr">tiny-lr</a>. It allows you to setup a LiveReload server. Even though it's early in development it works very well already.

over 1 year ago ·

Thanks for this, nice and to the point i love it.

over 1 year ago ·