ericclemmons / grunt-express-server

Grunt task for running an Express Server that works great with LiveReload + Watch/Regarde
MIT License
248 stars 64 forks source link

Can't run 'express' and 'watch' at the same time... #74

Closed nicroto closed 9 years ago

nicroto commented 10 years ago

Hi,

I am having trouble getting express and watch running concurrently. The idea is to restart the server anytime the code has changed.

First of all, the express task doesn't start a server on port 5000. Why is that? And secondly, watch can't be started after express begins.

Here is my watch and express configs and the composite task:

grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),

    ...

    watch: {
        client: {
            files: ['src/game/**/*', 'src/client/**/*'],
            tasks: ['devRebuild']
        },
        server: {
            files:  [ 'src/server/**/*', '!src/server/client/**/*' ],
            tasks:  [ 'express:server' ],
            options: {
                spawn: false
            }
        }
    },
    express: {
        options: {},
        server: {
            options: {
                script: 'src/server/server.js'
            }
        }
    }
});

// start develop
grunt.registerTask('devRebuild', ['clean', 'browserify:game_debug', 'stylus', 'copy']);
grunt.registerTask('dev', ['devRebuild', 'express', 'watch']);

And this is the server.js:

var pathUtils = require('path'),
    express = require("express"),
    bodyParser = require("body-parser"),
    app = express();

app.use( bodyParser.json() );

app.use( express.static( pathUtils.resolve( __dirname, "client" ) ) );

app.listen( process.env.PORT || 5000 );

And this is the output from running 'dev':

$ grunt dev

Running "clean:all" (clean) task
>> 0 paths cleaned.

Running "browserify:game_debug" (browserify) task

Running "stylus:compile" (stylus) task
File src/server/client/css/main.css created.

Running "copy:main" (copy) task
Created 2 directories, copied 2 files

Running "express:server" (express) task
Starting background Express server

I think the problem comes from the inability to actually start the server -> then it can detect a server started -> can't move on to the next grunt task. But why doesn't it start the server?

Thanks.

nicroto commented 10 years ago

Using searchcode.com I found another project using the grunt-express-server and watch.

I stole this trick and now I am not blocked:

var server = http.createServer(app).listen(PORT, function() {
    console.log('Express server listening on port ' + PORT);
});

Now I get what you meant by:

By default, unless delay or output has been customized, the server is considered "running" once any output is logged to the console, upon which control is passed back to grunt.

But this doesn't tell me that my code is responsible for logging something in the terminal. And in my case - it wouldn't work, because I am currently just serving a static website. So my watch will not be working while I change the content to be served (which is regenerated by that same watch), because my server will not be logging anything in the terminal.

I will send a pull request with some additional explanation, promptly. But this probably must be improved in general with some other way to detect that the server is up.

Thank you for the useful module.

nicroto commented 10 years ago

Here is my pull request with proposed change in the docs #75.

P.S.

drawveloper commented 9 years ago

The PR was accepted.

ismaels commented 9 years ago

Hi guys, I just run into the same problem described here and even though I followed the instructions, I couldn't the it working. After a few cycles of trying and revising everything I found out that it does not work if you have background: false in you express task.

express: {
            dev: {
                options: {
                    port: 3000,
                    background: false,
                    script: './index.js'
                }
            }
        },