简体   繁体   中英

What is process.env.UNIVERSAL in this node.js code

I'm working through a react with node tutorial and i have some code that looks like this:

if(process.env.UNIVERSAL){

  markup = ReactDOMServer.renderToString(...)

  .
  .
  .
}

I understand that process.env stores environment variables but i'm not sure what UNIVERSAL is or where it comes from. I tried to print it out in this code:

const port = process.env.PORT || 3000;
const env = process.env.NODE_ENV || 'production';
server.listen(port, err => {
 if (err) {
  return console.error(err);
 }
console.info(process.env.UNIVERSAL + ' test');
console.info(`Server running on http://localhost:${port} [${env}]`);
});

But it comes out undefined. Any idea what process.env.UNIVERSAL is?

While running a node.js code, you can specify the environment variables. Say for example you have your node.js server in app.js file. You can specify the env parameters while running like

NODE_ENV=development node app.js

for a UNIX environment

or

SET NODE_ENV=development & node app.js

for Windows environment

and then you can access it in your scripts with process.env.NODE_ENV

Similarly you can specify the environment UNIVERSAL while running the script

like

UNIVERSAL=test node app.js

Since you are not specifying anything, you get its value is undefined in the script

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM