q-nx-w
Last Updated: February 25, 2016
·
23.69K
· usharf
4740618b62a89362864b44dfe7bdfdbd

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();
....
Say Thanks
Respond

2 Responses
Add your response

3492
B26ec3c2769168c2cbc64cc3df9cdd9c

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 ·
15071
Bab15fcabd71e892f6307c6417037b23

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

over 1 year ago ·