1

I have done like here: What's the purpose of gruntjs server task?

In gruntfile, config:

    server: {
        port: 80,
        base: yeomanConfig.app
    }
});

Then

grunt.registerTask('server', 'Start a custom web server.', function() {
      var done = this.async();
      grunt.log.writeln('Starting web server on port 80.');
      require('./server/test.js').listen(80).on('close', done);
});

Test.js:

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

When I go to http://localhost/ I get: "Cannot GET /" error; Why?

I even tried another tutorial:

grunt.registerTask('server', 'Start a custom web server.', function() {
    grunt.task.run([
        'clean:server',
        'devcode:server',
        'concurrent:server',
        'autoprefixer',
        'watch'
    ]);
    var done = this.async();
    require('./server/test.js').listen(80).on('close', done);
});

But doesn't work too.

Community
  • 1
  • 1
Totty.js
  • 15,563
  • 31
  • 103
  • 175

2 Answers2

4

The reason is that you're not telling express to serve anything. You can see you'll get the exact same message if you just make a simple app like this and fetching localhost:8080:

var express = require('express');
var app = express();    
app.listen(8080);

In your Grunt file you need to at least set up some static paths or some routes (and live reload if you want that):

grunt.registerTask('server', 'Start a custom web server.', function() {
    grunt.task.run([
        'clean:server',
        'devcode:server',
        'concurrent:server',
        'autoprefixer',
        'watch'
    ]);
    var server = require('./server/test.js');
    server.use(require('connect-livereload')({
        port: 35729
    }));
    server.use(require('express').static(yeomanConfig.dist));
    server.listen(80);
});

Also in my case, I at least didn't need the async task and having it prevented the livereload from working.

To use node-inspector in conjunction with grunt, just launch grunt explicitly using node:

node --debug `which grunt` server

Then you can run node-inspector and connect as usual

ksimons
  • 3,797
  • 17
  • 17
  • Thanks, now it works. (you still have to put module.exports=app in the test.js file). But the node-inspector doesnt work. – Totty.js Oct 18 '13 at 16:35
  • Error: connect ECONNREFUSED Is node running with --debug port 5858? – Totty.js Oct 18 '13 at 16:39
  • I also added: var pathToApp = path.resolve(__dirname + '/../app'); var pathToTmp = path.resolve(__dirname + '/../.tmp'); app.use('/', express.static(pathToApp)); app.use('/', express.static(pathToTmp)); for using it with the source files – Totty.js Oct 18 '13 at 16:42
  • Edited with node-inspector instructions. – ksimons Oct 18 '13 at 17:15
  • I get: cannot find module "which". I don't really understand your command – Totty.js Oct 21 '13 at 14:47
  • I think which grunt is very vague: <<>> is better – Totty.js Oct 21 '13 at 15:00
  • Sometimes it fails to load the "index.css" what might be the problem? thanks – Totty.js Oct 22 '13 at 10:25
  • @pilau it executes everything inside of the ` ` and then substitutes the return value. So in my example, it would first execute `which grunt` and that would return something like /usr/local/bin/grunt. Then it would execute node --debug /usr/local/bin/grunt server. More info for Bash here: http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_04.html – ksimons Nov 24 '13 at 19:57
2

This helped me with the same issue: use grunt-express-server

<root-dir>: npm install grunt-express-server --save-dev

You can point the extension to your server.js file (which also starts listening) and can configure it to run with debug.

In your gruntfile.js:

grunt.loadNpmTasks('grunt-express-server');
...
  grunt.initConfig({ ...
    express: {
      options: {
        port: 3000,
        debug: true
      },
      server: {
        options: {
          script: 'server/server.js'
        }
      }
    }, ...

In your server.js:

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

app.configure(function () {
    app.set('port', process.env.PORT || 3000);
});

... 
//define routes
...

app.listen(app.get('port'));
console.log('Listening on port ' + app.get('port'));
Daniel Miller
  • 331
  • 1
  • 2
  • 9