grunt-express-server
grunt-express-server copied to clipboard
Allow setting of arbitrary process.env variables
Hi Eric,
Thank you for this useful grunt task. Great work!
Currently, grunt-express-server
supports overriding two environment variables, PORT and NODE_ENV. I feel it'd be nice to extend this and allow overriding of more variables, or allow setting of arbitrary variables.
My solution
Such overrides can be achieved by setting an env
option object. The port
and node_env
options will be retained for backwards-compatibility, but they will be overriden if a different value is specified inside the env
option. So basically something like this:
express: {
options: {
port: 3000,
node_env: 'production',
env: {
DEBUG: 'server', //Sets a new variable
PORT: 3001 //Overrides the value set using the port option above
}
}
}
Then, inside lib/server.js
, I plan on extending the env
object passed in to the call to grunt.util.spawn
with the values supplied by the user. This can be implemented the way underscore.js implements extend()
.
If you agree, I'm willing to submit a pull request!
My use case
I was trying to get the grunt-express-server
task to work with boilerplate code generated by the official express generator.
The boilerplate server requires an environment variable (DEBUG) to be set, for it to log anything out to the command line. You mention in the README that none of the subsequent tasks will be executed if nothing's logged out to the command line by the server.
I found no way of setting arbitrary environment variables with grunt-express-server
's options, so I hacked this feature into my local copy.
@c-das I whole-heartedly agree! Although, I would like to introduce your change as a major version upgrade with one additional change (which I can take care of):
- The removal of htmlmin.
Our changes together would be a well-earned major version to this project.
Let me know what you think. Thanks!
Sure, sounds fantastic! This way I also get time to think about unit testing.
PS: Sorry about the late response, I was away for the weekend.
Hi,
I've started working on this over at https://github.com/c-das/grunt-express-server/tree/adds-env-option . I'll submit a PR to your major version branch when it's ready.
what about DEBUG=server grunt
?
In case you're using DEBUG=server grunt
you're also need to modify ./bin/www (if you've generated project using express-generator), so it contains line debug.log = console.log.bind(console);
so debug module sends messages to stdlog instead of stderr, otherwise watch task won't be started before first request to application.
Would someone be so kind as to post a working grunt file example here, along with an edited www file (if necessary)?
I followed the express generator as well and am trying to get the config right.
Gruntfile (showing relevant bits):
grunt.initConfig({
watch: {
express: {
files: [
'app.js',
'bin/www',
'public/**/*.js',
'public/**/*.cs',
'routes/*.js',
'views/*.jade'
],
tasks: ['express:dev'],
atBegin: true,
spawn: false
}
},
express: {
dev: {
options: {
script: './bin/www'
}
}
}
});
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-express-server');
grunt.registerTask('server', ['watch:express']);
Grunt correctly finds the www file and starts it however, for some strange reason, none of the CSS / styling appears when loading a page, and as soon as I hit the server it crashes (so any subsequent calls fail, since the server has stopped after the initial one).
This same behaviour occurred even if I add the debug server env variable, like so:
env: {
DEBUG: 'server'
}
Any help really appreciated - also happy to make a commit to clean up the read me and make turn this Q&A into a guide on the landing repo page :+1: :smile:
@ericclemmons @cdax ericclemmons Any update on this issue or timetable when this feature will get released?