【问题标题】:Node.js : how to pass parameter's value from terminal to JS scriptNode.js:如何将参数的值从终端传递到 JS 脚本
【发布时间】:2014-10-02 22:09:30
【问题描述】:

给定一个基于jsdomsvgcreator.node.js 脚本文件:

var jsdom = require('jsdom');
jsdom.env(
  "<html><body></body></html>",        // CREATE DOM HOOK
  [ 'http://d3js.org/d3.v3.min.js',    // JS DEPENDENCIES online ...
  'js/d3.v3.min.js' ],                 // ... & offline
// D3JS CODE * * * * * * * * * * * * * * * * * * * * * * * *
  function (err, window) {
    var svg = window.d3.select("body")
        .append("svg")
        .attr("width", 100)
        .attr("height", 100);
    svg.append("rect")
        .attr("id", "rect1")
        .attr("x", 10)
        .attr("y", 10)
        .attr("width", 80)
        .attr("height", 80)
        .style("fill", "green");
    // END svg design

  //PRINTING OUT SELECTION
    console.log(window.d3.select("body").html());
 }
// END (D3JS) * * * * * * * * * * * * * * * * * * * * * * * *
);

假设我使用 NodeJS 终端命令运行它并生成一个output.svg 文件:

node svgcreator.node.js > output.svg  # nodeJS + script command

如何将参数的值从终端传递给 NodeJS?


测试的依赖:


使用的解决方案 (@Matt_Harrison): 我们依赖 process.env.myVar

svgcreator.node.jsJS代码:

var jsdom = require('jsdom');
jsdom.env(
  "<html><body></body></html>",        // CREATE DOM HOOK:
  [ 'http://d3js.org/d3.v3.min.js',    // JS DEPENDENCIES online ...
  'js/d3.v3.min.js' ],                 // ... & offline
// D3JS CODE * * * * * * * * * * * * * * * * * * * * * * * *
  function (err, window) {

    var color = process.env.COLOR;     // <<################# IMPORTANT !!
    var svg = window.d3.select("body")
        .append("svg")
        .attr("width", 100)
        .attr("height", 100);
    svg.append("rect")
        .attr("id", "rect1")
        .attr("x", 10)
        .attr("y", 10)
        .attr("width", 80)
        .attr("height", 80)
        .style("fill", color);         // <<################# IMPORTANT !!
    // END svg design

  //PRINTING OUT SELECTION
    console.log(window.d3.select("body").html());
 }
// END (D3JS) * * * * * * * * * * * * * * * * * * * * * * * *
);

终端 NodeJS 命令:

COLOR=#66AAFF node svgcreator.node.js > out.svg   # <<############# IMPORTANT !! setting the value.

+1 @Matt_Harrison 的回答和赞赏的问题!

【问题讨论】:

  • 我不太确定您是否只是在寻找这个,但是..您可以使用 process.argv 将参数从终端传递到浏览器
  • 我想将参数从终端传递到我的脚本jsdom.node.js,它在nodejs JS引擎中运行。
  • 对不起!我也是 node.js 的新手,但我很确定,您可以使用 process.argv 数组将参数从终端传递到 js 文件。例如。 node test.js 2 在终端和 js 文件中,您可以像 console.log(process.argv[2]) 一样获得它。希望有帮助
  • 欢迎您的好意,我们在尝试中学习~

标签: node.js svg


【解决方案1】:

在你的终端中,你可以使用environment variables:

$ COLOR=#FFFFFF node jsdom.node.js

在你的 JS 中,做:

var color = process.env.COLOR;

或者您可以在命令中添加额外的参数:

$ node jsdom.node.js '#FFFFFF'

在你的 JS 中:

var color = process.argv[2];

如果你想使用库;我建议查看 Minimist 库或 Commander 以获得功能更全面的解决方案。

【讨论】:

  • 当您说This will then be available 时,您的意思是within the js / d3js script 对吗? (只是检查)。
  • 来了(晚餐在这里)。我将在我的 JS 中添加一些 var color = process.env.COLOR;,然后运行它。
  • n⁰1 作品!我编辑了您的答案以使其更清楚。
  • 好的,已验证!答案是完全有效的,并且已经改进为一目了然。
  • 谢谢,很高兴能帮上忙。
猜你喜欢
  • 2014-10-27
  • 2015-05-04
  • 2022-01-23
  • 2023-01-19
  • 1970-01-01
  • 2020-07-10
  • 1970-01-01
  • 2022-09-27
  • 1970-01-01
相关资源
最近更新 更多