I use different ports and database info for test/production servers
and was wondering what the best way to make this a bit more automated
is.
Thanks
--
Guy Halford-Thompson - http://www.cach.me/blog
Look at process.env?
----
Aria Stewart
Ty
> --
> You received this message because you are subscribed to the Google Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com.
> To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.
I understand how you can access
....
process.env['JOBS']
...
but how do I set the variable 'JOBS' on my server? This cannot be
done in my node files as they will be the same on all servers.
Thanks for the help
G
> Sorry, I should have been a bit more specific in my question... I was
> wondering how to set the variables on the server, not access them in
> node
>
> I understand how you can access
> ....
> process.env['JOBS']
> ...
>
> but how do I set the variable 'JOBS' on my server? This cannot be
> done in my node files as they will be the same on all servers.
Depends on your shell's syntax for environment variables (google
that), but typically:
$> JOBS=123
$> export JOBS
$> node
> process.env["JOBS"]
'123'
Corey
Let's say you have this in your server.js file:
if (process.env.SERVER === "PRODUCTION") { .. produce stuff .. }
There are many ways to set environment variables.
You can set them on the command line for just one command:
$ SERVER=PRODUCTION node server.js
You can export them into your shell environment:
$ export SERVER=PRODUCTION
$ node server.js
You can export them from a shell script:
# foo.sh
export SERVER=PRODUCTION
$ source foo.sh # not ./foo.sh! that would run in a subshell
$ node server.js
You can pass a key-value list to the "env" command:
$ echo SERVER=PRODUCTION >> server-profile
$ echo FOO=bar >> server-profile
$ env $(cat server-profile) node server.js
If you're starting your program with node's child_process.spawn, you
can pass them on the options object:
child_process.spawn("node", ["server.js"], { env: { SERVER: "PRODUCTION" }})
If you're starting your program with execp in C, you can use the
"environ" extern.
extern char **environ;
int main (int argc, char** argv) {
char **saved_env;
int ret;
saved_env = environ;
environ = { "SERVER=PRODUCTION" };
ret = execvp("node", { "server.js" });
environ = saved_env;
return ret;
}
If you're loading your program from your bash login shell, you can put
this in your ~/.bashrc:
export SERVER=PRODUCTION
If you're running it with SMF, you can put this in your manifest xml:
<method_environment>
<envvar name="SERVER" value="PRODUCTION"/>
</method_environment>
There are probably other ways to set environment vars. "man sh" and
"man bash" have sections on this.
Environs are a pretty core Unix feature. Think of it like another set
of arguments to your program.
--i
Hi Isaac,
Thanks for your detailed response, that makes it very clear. Would it
be worth adding this to the documentation at some point? Or is it just
be being v inexperienced...
Thanks also to everyone else for you help.
Guy
Thanks for your detailed response, that makes it very clear. Would it
be worth adding this to the documentation at some point? Or is it just
be being v inexperienced...
Thanks also to everyone else for you help.
Guy