通过URL获取执行本地bash脚本

时间:2018-06-05 21:49:54

标签: javascript bash fetch-api

我想知道如何通过利用fetch转换curl命令执行bash脚本并关联输入变量?

以下通过控制台完美运行。

  curl -s http://localhost:3001/ident.sh | bash /dev/stdin x627306090abab3a6e1400e9345bc60c78a8bef57 2 1019489767657645

但是每当我尝试使用fetch调用它时,甚至没有脚本参数:

  fetch("http://localhost:3001/ident.sh")
  .then((resp) => resp.json())
  .then((data) => {

      console.log(data)

  })

OR

  fetch("http://localhost:3001/ident.sh", {
    method: 'GET',
    headers : { 
        'Content-Type': 'application/json',
        'Accept': 'application/json' }
  })
  .then((resp) => resp.json()) // Transform the data into json
  .then((data) => {

      console.log(data)

  })

这导致:

Uncaught (in promise) SyntaxError: Unexpected token # in JSON at position 0

1 个答案:

答案 0 :(得分:2)

indent.sh没有返回JSON,它返回包含shell脚本的纯文本。如果您想看到它,请使用resp.text(),而不是resp.json()

这不会执行脚本。有关如何从Node.js执行命令,请参阅Execute a command line binary with Node.js